UTC datetime of a specific LocalDateTime

Viewed 166

Example: Get UTC datetime of 2020-06-04 12.00 AM IST

My application needs to compare the current datetime with the saved access-start time for login reasons. Since UTC avoids timezone issues, the given access-start datetime is converted to an UTC datetime and stored in my database.

Case 1: Access-start datetime: Current timestamp, which can be converted to UTC and stored.

Case 2: Access-start time: 2020-06-04 12.00 AM IST, where I'm not sure how to convert it to UTC.

1 Answers
    OffsetDateTime currentUtcTime = OffsetDateTime.now(ZoneOffset.UTC);

Store into a column of datatype timestamp with time zone in your SQL database. Retrieve back into an OffsetDateTime.

No need to involve your own time zone nor to convert between time zones.

(Code is not tested, forgive typos.)

Related