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?
-
Possible duplicate of [How to convert from java.sql.Timestamp to java.util.Date?](https://stackoverflow.com/questions/10621451/how-to-convert-from-java-sql-timestamp-to-java-util-date) – vinS Dec 13 '17 at 05:27
-
2Dates and Timestamps do not have any intrinsic formatting. Consider using java8 classes or SimpleDateFormat – Scary Wombat Dec 13 '17 at 05:28
-
@ScaryWombat This is more of a DynamoDB question from what I can tell. But even there, dates also probably have no intrinsic formatting. – Tim Biegeleisen Dec 13 '17 at 05:30
-
@TimBiegeleisen Hi Tim, yes you are correct. Maybe it is not even valid to the question and should be marked as a dupe as indicated by vinS. I did not read the Q proeprly. – Scary Wombat Dec 13 '17 at 05:34
-
I'd be shocked if the DynanoDB driver for Java can't automatically marshall a timestamp. – Tim Biegeleisen Dec 13 '17 at 05:34
-
2Why have you got a `java.sql.Timestamp` object? The class (along with `Calendar`, `Date` and their friends) is long outmoded. If you can, I recommend you use `java.time.Instant` instead. `java.time` is the modern Java date and time API, it is a lot nicer to work with than the old classes. – Ole V.V. Dec 13 '17 at 10:17
-
1Working with some legacy code and changing it would require me to changing it else where also – pannu Dec 14 '17 at 12:46
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

- 81,772
- 15
- 137
- 161
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;
}

- 502,043
- 27
- 286
- 360
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.

- 13,640
- 5
- 54
- 83