5

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?

7
  • Possible duplicate of How to convert from java.sql.Timestamp to java.util.Date? Commented Dec 13, 2017 at 5:27
  • 2
    Dates and Timestamps do not have any intrinsic formatting. Consider using java8 classes or SimpleDateFormat Commented Dec 13, 2017 at 5:28
  • @ScaryWombat This is more of a DynamoDB question from what I can tell. But even there, dates also probably have no intrinsic formatting. Commented Dec 13, 2017 at 5:30
  • 2
    Why 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. Commented Dec 13, 2017 at 10:17
  • 1
    Working with some legacy code and changing it would require me to changing it else where also Commented Dec 14, 2017 at 12:46

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

Comments

4

You can call .toInstant().toString() on the java.sql.timestamp to get an ISO-8601 time string.

Comments

2

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

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.