None of the above answers actually explained the problem. The real problem is that the initial assumptions were incorrect. The timestamp from the database was created using the JVM Asia/Kolkata local time zone and not UTC. This is the default behavior for JDBC, so he still recommends setting the JVM time zone to UTC.
If the timestamp from the database was actually:
2013-09-25 11:27:34 AM UTC
Or in ISO-8601 format:
2013-09-25T11:27:34Z
Then using new DateTime(timestamp, DateTimeZone.UTC) works fine. Look at yourself:
Timestamp timestamp = new Timestamp(1380108454000L); DateTime dt = new DateTime(timestamp.getTime(), DateTimeZone.UTC); System.out.println(dt);
If you're curious where I got 1380108454000L , I just used the Joda parsing classes:
ISODateTimeFormat.dateTimeParser().parseMillis("2013-09-25T11:27:34Z")
Alternatively there are websites where you can enter the date, time and time zone, and it returns the epoch value in milliseconds or vice versa. This is sometimes good as a sanity check.
// https://www.epochconverter.com Input: 1380108454000 Click: "Timestamp to Human Date" Assuming that this timestamp is in milliseconds: GMT: Wednesday, September 25, 2013 11:27:34 AM
Also, keep in mind that the java.sql.Timestamp class roughly corresponds to the Joda / Java 8+ Instant class. It is sometimes easier to convert between equivalent classes to detect errors like before.
nogridbag Jul 23 '19 at 5:26 2019-07-23 05:26
source share