Java 8 – Convert LocalDateTime to Epoch Milliseconds

Introduction

In Java 8, when dealing with date and time, you may encounter situations where you need to convert a LocalDateTime object into the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT). This is often required when interfacing with systems that represent time as a long value or for storing date and time information in a database in a compact form. The java.time package introduced in Java 8 provides a straightforward way to perform this conversion.

In this guide, we’ll walk through the process of converting a LocalDateTime to epoch milliseconds using Java 8’s java.time package.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Convert LocalDateTime to Epoch Milliseconds
    • Convert Epoch Milliseconds Back to LocalDateTime
  • Advanced Considerations
  • Conclusion

Problem Statement

The task is to create a Java program that:

  • Converts a LocalDateTime object to the number of milliseconds since the Unix epoch.
  • Optionally, converts the epoch milliseconds back into a LocalDateTime object.

Example:

  • Input: A LocalDateTime object representing 2023-08-28T14:30:45.
  • Output: A long value representing the epoch milliseconds.

Solution Steps

  1. Use ZoneOffset.UTC: Determine the time zone offset (typically UTC) to ensure the correct conversion.
  2. Convert LocalDateTime to Instant: Use the atZone() method to convert LocalDateTime to an Instant.
  3. Get Epoch Milliseconds: Use toEpochMilli() to obtain the epoch milliseconds.
  4. Reverse the Process: Optionally, convert the milliseconds back to LocalDateTime.

Java Program

Convert LocalDateTime to Epoch Milliseconds

The following example demonstrates how to convert a LocalDateTime to epoch milliseconds.

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

/**
 * Java 8 - Convert LocalDateTime to Epoch Milliseconds
 * Author: https://www.rameshfadatare.com/
 */
public class LocalDateTimeToEpochMillis {

    public static void main(String[] args) {
        // Step 1: Create a LocalDateTime object
        LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 28, 14, 30, 45);

        // Step 2: Convert LocalDateTime to Instant using UTC time zone
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);

        // Step 3: Get the epoch milliseconds
        long epochMillis = instant.toEpochMilli();

        // Step 4: Display the epoch milliseconds
        System.out.println("Epoch Milliseconds: " + epochMillis);
    }
}

Output

Epoch Milliseconds: 1693239045000

Explanation

  • LocalDateTime.of(2023, 8, 28, 14, 30, 45) creates a LocalDateTime instance for the specified date and time.
  • toInstant(ZoneOffset.UTC) converts the LocalDateTime to an Instant object, which represents an instantaneous point on the timeline.
  • toEpochMilli() retrieves the number of milliseconds since the Unix epoch from the Instant.

Convert Epoch Milliseconds Back to LocalDateTime

If you have an epoch milliseconds value and need to convert it back to a LocalDateTime, you can do so as follows:

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

/**
 * Java 8 - Convert Epoch Milliseconds to LocalDateTime
 * Author: https://www.rameshfadatare.com/
 */
public class EpochMillisToLocalDateTime {

    public static void main(String[] args) {
        // Step 1: Define epoch milliseconds
        long epochMillis = 1693239045000L;

        // Step 2: Convert epoch milliseconds to Instant
        Instant instant = Instant.ofEpochMilli(epochMillis);

        // Step 3: Convert Instant to LocalDateTime using UTC time zone
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);

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

Output

LocalDateTime: 2023-08-28T14:30:45

Explanation

  • Instant.ofEpochMilli(epochMillis) creates an Instant from the epoch milliseconds.
  • LocalDateTime.ofInstant(instant, ZoneOffset.UTC) converts the Instant back to a LocalDateTime using the UTC time zone.

Advanced Considerations

  • Time Zones: The conversion from LocalDateTime to Instant is dependent on the time zone. Always ensure that the correct time zone is applied (typically UTC) to avoid incorrect conversions.

  • Precision: The epoch milliseconds are precise to the millisecond. If higher precision (nanoseconds) is required, you may need to work with Instant directly or consider using System.nanoTime().

  • Thread Safety: The LocalDateTime and Instant classes are immutable and thread-safe, making them suitable for use in multi-threaded environments.

Conclusion

This guide covers the process of converting a LocalDateTime to epoch milliseconds and vice versa in Java 8. The java.time package provides a robust and flexible framework for working with date and time in Java, ensuring that conversions between different representations of time are simple and accurate.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top