Java Convert Epoch Days to LocalDateTime

Tags: Java LocalDateTime Java 8

In this Java core tutorial we learn how to convert number of epoch days to a LocalDateTime object in Java programming language.

How to convert Epoch Days to LocalDateTime in Java

In Java, with a given number of epoch days which is an incrementing count of days where day 0 is 1970-01-01, we can use the following steps to convert it to a LocalDateTime object.

  • Step 1: use the LocalDate.ofEpochDayy(long epochDay) to convert the epoch days to a LocalDate object
  • Step 2: use the LocalDate.atStartOfDay() method to convert the LocalDate object in step 1 to a LocalDateTime object.

import java.time.LocalDate;
import java.time.LocalDateTime;

public class ConvertEpochDayToLocalDateTimeExample {
    public static void main(String... args) {
        long epochDays = 19184;

        // Convert Epoch Days to LocalDateTime object
        LocalDate localDate = LocalDate.ofEpochDay(epochDays);
        LocalDateTime localDateTime = localDate.atStartOfDay();

        System.out.println("Epoch Days: " + epochDays);
        System.out.println("LocalDateTime: " + localDateTime);
The output as below.
Epoch Days: 19184
LocalDateTime: 2022-07-11T00:00

Happy Coding 😊

Java Convert Epoch Seconds to LocalDate

Java Convert Epoch Milliseconds to LocalDate

Java Convert Epoch Milliseconds to LocalDateTime