Java Convert Epoch Seconds to LocalDate
Tags: Java LocalDate Java 8
In this Java core tutorial we learn how to convert an epoch seconds value to a LocalDate object using date time API of Java programming language.
How to convert Epoch Seconds to LocalDate in Java
The epoch seconds value is the number of seconds from the epoch of 1970-01-01T00:00:00Z. In Java, with a given epoch seconds we can use these steps to convert it to a LocalDate object.
- Step 1: get default system time offset.
- Step 2: use the LocalDateTime.ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) method to convert epoch seconds value with the default system time offset from step 1 to LocalDateTime object.
- Step 3: use the LocalDateTime.toLocalDate() convert the LocalDateTime object from step 2 to a LocalDate object.
ConvertEpochSecondToLocalDateExample1.java
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class ConvertEpochSecondToLocalDateExample1 {
public static void main(String... args) {
long epochSeconds = 999888777;
int nanosOfSecond = 0;
// Get System Zone Offset
ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
// Convert Epoch Seconds to LocalDate object
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSeconds, nanosOfSecond, zoneOffset);
LocalDate localDate = localDateTime.toLocalDate();
System.out.println("Epoch Seconds: " + epochSeconds);
System.out.println("LocalDate: " + localDate);
}
}
Epoch Seconds: 999888777
LocalDate: 2001-09-08
Happy Coding 😊
Related Articles
Java Convert Epoch Days to LocalDateTime