Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: How to create a ZonedDateTime from an Epoch value?

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?

like image 606
rabejens Avatar asked Mar 02 '15 11:03

rabejens


People also ask

How do I get a ZonedDateTime zone?

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.

How do I parse LocalDateTime to ZonedDateTime?

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.

How do I get a zoneddatetime from an epoch value?

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

How to convert string to zoneddatetime in Java?

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.

How to get the zoneddatetime of a time zone?

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.

What is Java 8 zoneddatetime class?

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


1 Answers

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);
like image 92
Prisoner Avatar answered Oct 22 '22 12:10

Prisoner