Java 8 – Convert Epoch Time Milliseconds to LocalDate or LocalDateTime

Introduction

Epoch time, also known as Unix time, is the number of milliseconds that have elapsed since January 1, 1970 (midnight UTC). In Java 8, the new java.time package provides classes like LocalDate and LocalDateTime to handle date and time in a more flexible way. Converting epoch milliseconds to LocalDate or LocalDateTime is a common requirement when working with time-based data.

In this guide, we will learn how to convert epoch time in milliseconds to LocalDate or LocalDateTime using Java 8's Instant, ZoneId, and LocalDateTime.

Solution Steps

  1. Create Instant from Epoch Milliseconds: Use the Instant.ofEpochMilli() method to convert epoch time in milliseconds to an Instant.
  2. Convert to LocalDateTime: Use LocalDateTime.ofInstant() to convert the Instant to a LocalDateTime.
  3. Convert to LocalDate: Extract the LocalDate from the LocalDateTime or convert directly using LocalDate.

Java Program

Method 1: Convert Epoch Time to LocalDateTime

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

public class EpochToLocalDateTime {
    public static void main(String[] args) {
        // Step 1: Define epoch time in milliseconds (Example: 1625812800000L is 2021-07-09 00:00:00 UTC)
        long epochMilli = 1625812800000L;

        // Step 2: Convert epoch milliseconds to LocalDateTime
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());

        // Step 3: Display the LocalDateTime
        System.out.println("LocalDateTime: " + dateTime);
    }
}

Output

LocalDateTime: 2021-07-09T05:30

Explanation

  • Step 1: We define epoch time in milliseconds (1625812800000L), which represents the date 2021-07-09 in UTC.
  • Step 2: We use Instant.ofEpochMilli(epochMilli) to create an Instant from the epoch time, and then convert it to LocalDateTime using LocalDateTime.ofInstant(). The ZoneId.systemDefault() specifies the time zone of the system.
  • Step 3: The LocalDateTime is printed, which shows the date and time in the system's default time zone.

Method 2: Convert Epoch Time to LocalDate

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

public class EpochToLocalDate {
    public static void main(String[] args) {
        // Step 1: Define epoch time in milliseconds
        long epochMilli = 1625812800000L;

        // Step 2: Convert epoch milliseconds to LocalDate
        LocalDate date = Instant.ofEpochMilli(epochMilli)
                                .atZone(ZoneId.systemDefault())
                                .toLocalDate();

        // Step 3: Display the LocalDate
        System.out.println("LocalDate: " + date);
    }
}

Output

LocalDate: 2021-07-09

Explanation

  • Step 1: We define the same epoch time in milliseconds.
  • Step 2: We first convert the epoch time to an Instant using Instant.ofEpochMilli(), then convert it to a ZonedDateTime using .atZone(ZoneId.systemDefault()), and finally extract the LocalDate using .toLocalDate().
  • Step 3: The result is printed as a LocalDate, which shows only the date part.

Conclusion

Converting epoch time in milliseconds to LocalDate or LocalDateTime is simple with Java 8's Instant, LocalDateTime, and LocalDate classes. The key step is to use Instant.ofEpochMilli() to create an Instant and then convert it to the desired date or time format using LocalDateTime.ofInstant() or toLocalDate(). This approach ensures that the conversion takes into account the system's default time zone or any other time zone you may specify.

Comments