Java Convert Epoch Seconds to LocalDateTime

Tags: Java LocalDateTime Java 8

In this Java core tutorial we learn how to convert an epoch seconds value to a LocalDateTime object in Java programming language.

How to convert Epoch Seconds to LocalDateTime in Java

In Java, we can use the LocalDateTime.ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) method to instantiate a new LocalDateTime object from a given epoch seconds value which is the number of seconds from the epoch of 1970-01-01T00:00:00Z.

ConvertEpochSecondsToLocalDateTimeExample1.java

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class ConvertEpochSecondsToLocalDateTimeExample1 {
    public static void main(String... args) {
        long epochSeconds = 999888777;
        int nanosOfSecond = 0;
        ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());

        // Create LocalDateTime object from epoch seconds
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSeconds, nanosOfSecond, zoneOffset);

        System.out.println("Epoch seconds: " + epochSeconds);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}
The output as below.
Epoch seconds: 999888777
LocalDateTime: 2001-09-08T01:52:57

Happy Coding 😊

Java Convert LocalDateTime to Instant

Java Convert LocalDateTime to String

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to OffsetDateTime

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDateTime to Calendar

Java Convert LocalDateTime to Date

Java LocalDateTime by Examples

Java Convert LocalDateTime to Epoch Seconds