I want to know if there is a way to convert java.time.OffsetDateTime
to Milliseconds, I found this way, but I don't know if it is the best one:
book.getInteractionDuration().getStartTimeStamp().toEpochSecond()*1000
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. For example, the value "2nd October 2007 at 13:45.30. 123456789 +02:00" can be stored in an OffsetDateTime .
A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .
Converting Date to OffsetDateTimeDate date = new Date(); OffsetDateTime offsetDateTime = date. toInstant() . atOffset(ZoneOffset. UTC);
I would just convert the OffsetDateTime
to an Instant
and then use toEpochMilli
:
long millis = book.getInteractionDuration().getStartTimeStamp().toInstant().toEpochMilli();
Unlike toEpochSecond()
, this approach won't lose any more precision than is inherent in wanting milliseconds rather than nanoseconds.
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