I have an java.time.OffsetDateTime
which I would like to convert to a java.sql.Timestamp
. Since Timestamp
doesn't store any offset information, I am going to store all dates/times in the database as UTC.
How do I convert the OffsetDateTime
to a Timestamp
which is in UTC?
EDIT:
I believe this is the answer but it seems are rather convoluted way to covert to UTC:
OffsetDateTime dateTime = OffsetDateTime.now(); Timestamp timestamp = Timestamp.valueOf(dateTime.atZoneSameInstant(ZoneId.of("Z")).toLocalDateTime());
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 . OffsetDateTime is an immutable representation of a date-time with an offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich.
UTC time in ISO-8601 is 12:11:38Z.
This would be a way to do the conversion and ensure UTC is used. That I think is a little cleaner than solution proposed using the epoch seconds.
Timestamp test = Timestamp.valueOf(entityValue.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
Another solution would be:
Timestamp.valueOf(LocalDateTime.ofInstant(dateTime.toInstant(), ZoneOffset.UTC));
It converts the dateTime
to UTC, strips the timezone information and then converts the result to a Timestamp
. It is still convoluted but IMHO it's a bit cleaner.
Just using toInstance()
or toEpochSeconds()
will adjust the result with the offset provided.
The following shows the test results from this and the other answers:
OffsetDateTime dateTime = OffsetDateTime.of(2015, 10, 23, 12, 44, 43, 0, ZoneOffset.UTC); // OffsetDateTime.of(2015, 10, 23, 12, 44, 43, 0, ZoneOffset.ofHours(-5)); err.println("dateTime = " + dateTime ); err.println("as LocalDateTime = " + dateTime.toLocalDateTime() ); err.println("as timestamp (mine) = " + Timestamp.valueOf(LocalDateTime.ofInstant(dateTime.toInstant(), ZoneOffset.UTC)) ); err.println("@Cheetah (correct) = " + Timestamp.valueOf(dateTime.atZoneSameInstant(ZoneId.of("Z")) .toLocalDateTime()) ); err.println("@Notso (wrong) = " + Timestamp.from(dateTime.toInstant()) ); err.println("@Glorfindel (wrong) = " + new Timestamp(1000 * dateTime.toEpochSecond()) );
which gives the following results (my timezone is CET) :
(with ZoneOffset.UTC) dateTime = 2015-10-23T12:44:43Z as LocalDateTime = 2015-10-23T12:44:43 as timestamp (mine) = 2015-10-23 12:44:43.0 @Cheetah (correct) = 2015-10-23 12:44:43.0 @Notso (wrong) = 2015-10-23 14:44:43.0 @Glorfindel (wrong) = 2015-10-23 14:44:43.0 (with ZoneOffset.ofHours(-5)) dateTime = 2015-10-23T12:44:43-05:00 as LocalDateTime = 2015-10-23T12:44:43 as timestamp (mine) = 2015-10-23 17:44:43.0 @Cheetah (correct) = 2015-10-23 17:44:43.0 @Notso (wrong) = 2015-10-23 19:44:43.0 @Glorfindel (wrong) = 2015-10-23 19:44:43.0
(The version from Notso above was before his edit of Feb 17 2016)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With