Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert OffsetDateTime to UTC Timestamp

Tags:

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()); 
like image 290
Cheetah Avatar asked Jun 04 '15 18:06

Cheetah


People also ask

Is OffsetDateTime a UTC?

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.

What is UTC timestamp now?

UTC time in ISO-8601 is 12:11:38Z.


2 Answers

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()); 
like image 183
Notso Avatar answered Sep 18 '22 18:09

Notso


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)

like image 42
rve Avatar answered Sep 19 '22 18:09

rve