Java Convert Epoch Milliseconds to LocalDate
Tags: Java LocalDate Java 8
In this Java core tutorial we learn how to convert an epoch milliseconds value to a LocalDate object using date time API in Java programming language.
How to convert Epoch Milliseconds to LocalDate in Java
Epoch seconds is the number of milliseconds from 1970-01-01T00:00:00Z. In Java, with a given epoch seconds value we can use these steps to convert it to a LocalDate object.
- Step 1: use the Instant.ofEpochMilli(long epochMilli) method to convert epoch milliseconds to Instant object.
- Step 2: use Instant.atZone(ZoneId zone) to convert the Instant object from step 1 to a ZonedDateTime object in system default time zone.
- Step 3: use the ZonedDateTime.toLocalDate() to convert the ZonedDateTime object at step 2 to LocalDate object.
ConvertEpochMillisecondsToLocalDateExample1.java
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ConvertEpochMillisecondsToLocalDateExample1 {
public static void main(String... args) {
long epochMilliseconds = 1655227162222L;
Instant instant = Instant.ofEpochMilli(epochMilliseconds);
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
LocalDate localDate = zonedDateTime.toLocalDate();
System.out.println("Epoch Milliseconds: " + epochMilliseconds);
System.out.println("LocalDate: " + localDate);
}
}
Epoch Milliseconds: 1655227162222
LocalDate: 2022-06-15
Happy Coding 😊
Related Articles
Java Convert Epoch Days to LocalDateTime