I have to store updated time in dynamodb, while going through the document of dynamodb found out we can build an index if we store it in the format of ISO 8601. But in application I have java.sql.timestamp. How can I convert it?
4 Answers
java.time
First answer, don’t use java.sql.Timestamp. This class is long outmoded. Get an Instant instead. Instant is one of the classes in java.time, the modern Java date and time API also known as JSR 310. This API is so much nicer to work with than the old classes. Assuming inst is your Instant:
String iso8601String = inst.toString();
System.out.println(iso8601String);
On my computer this just printed
2017-12-13T11:04:59.282Z
The toString methods of the modern classes produce ISO 8601 format, which makes your task very easy.
Convert your Timestamp
Since (as I understand) you get your Timestamp object from legacy code that you cannot afford to upgrade just now: The first thing to do is to convert it to an Instant. Then you may proceed as above.
I assume that ts is your timestamp. If you are using Java 8 or later:
String iso8601String = ts.toInstant().toString();
If you are using Java 6 or 7, you will need to get the ThreeTen Backport (link below). This is the backport of JSR 310 to the mentioned Java versions. Since in these versions, the conversion method isn’t built into the Timestamp class, the conversion happens a little differently, everything else is the same. I have not tested the following code line, so please bear with any typo.
String iso8601String = DateTimeUtils.toInstant(ts).toString();
Links
Comments
I'm not sure you necessarily have to do any heavy manual lifting here, as I would expect a good driver to be able to handle this. Assuming you do need a manual conversion from java.sql.timestamp to an ISO 8601 compliant string, you could try the following:
public String getISOTimestamp(java.sql.timestamp ts) {
Date date = new Date(ts.getTime());
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String isoTS = sdf.format(date);
return isoTS;
}
Comments
DyanmoDB itself is schemaless (you dont define non-key attribute types up front) and it and does not recognise date as a type.. You can use a String in Dynamo to store dates
You can use the string data type to represent a date or a time stamp. One way to do this is by using ISO 8601 strings, as shown in these examples:
2016-02-15
2015-12-21T17:42:34Z
20150311T122706Z
If you are using an object modelling library, like DynamoDBMapper, it will do some type marshalling on your behalf. For example DynamoDBMapper handles many Java data types including
This section describes the supported primitive Java data types, collections, and arbitrary data types.
DynamoDB supports the following primitive data types and primitive wrapper classes.
String
Boolean, boolean
Byte, byte
Date (as ISO_8601 millisecond-precision string, shifted to UTC)
Calendar (as ISO_8601 millisecond-precision string, shifted to UTC)
Long, long
Integer, int
Double, double
Float, float
BigDecimal
BigInteger
So you can use something like DynamoDBMapper to marshall data between dates in your code and Strings in your database, or you could just do this yourself. In either case the data in DynamoDB is just Strings, and your index is ordered as a String. Using ISO 8601 format just means your Strings are ordered naturally.
java.sql.Timestampobject? The class (along withCalendar,Dateand their friends) is long outmoded. If you can, I recommend you usejava.time.Instantinstead.java.timeis the modern Java date and time API, it is a lot nicer to work with than the old classes.