Java 8's LocalDateTime
has an ofEpochSecond
method. Unfortunately, there is no such method in the ZonedDateTime
. Now, I have an Epoch value and an explicit ZoneId
given. How do I get a ZonedDateTime
out of these?
The getZone() method of a ZonedDateTime class is used to get the time-zone from this ZonedDateTime. This method returns the ZoneId such as 'Asia/Calcutta'. Parameters: This method does not take any parameters. Return value: This method returns a ZoneId representing the time-zone.
Convert LocalDateTime to ZonedDateTime The LocalDateTime has no time zone; to convert the LocalDateTime to ZonedDateTime , we can use . atZone(ZoneId. systemDefault()) to create a ZonedDateTime containing the system default time zone and convert it to another time zone using a predefined zone id or offset.
Unfortunately, there is no such method in the ZonedDateTime. Now, I have an Epoch value and an explicit ZoneId given. How do I get a ZonedDateTime out of these? You should be able to do this via the Instant class, which can represent a moment given the epoch time. If you have epoch seconds, you might create something via something like
Use plus () method add n no of a specific time units to the ZonedDateTime. Use minus () method to go back to previous dates or time units. This method will subtract the time unit on the given date. Use ZonedDateTime.parse () method to convert string to ZonedDateTime in java.
Number of whole seconds since the epoch reference date of first moment of 1970 in UTC Pass those numbers to the factory method Instant.ofEpochSecond (long epochSecond, long nanoAdjustment) With an Instant in hand, proceed to assign a time zone to get a ZonedDateTime.
Java 8 ZonedDateTime represents a date and time with timezone information in ISO-8601 calendar system and stores all date to a precision of nanoseconds. Skip to content HowToDoInJava Related Posts Menu Menu Java Spring Boot Python Dark Mode Related Posts Java ZonedDateTime class Last Updated: December 26, 2020 Java Date Time
You should be able to do this via the Instant
class, which can represent a moment given the epoch time. If you have epoch seconds, you might create something via something like
Instant i = Instant.ofEpochSecond(t);
ZonedDateTime z = ZonedDateTime.ofInstant(i, zoneId);
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