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
LocalDateTimeto Epoch Milliseconds - Convert Epoch Milliseconds Back to
LocalDateTime
- Convert
- Advanced Considerations
- Conclusion
Problem Statement
The task is to create a Java program that:
- Converts a
LocalDateTimeobject to the number of milliseconds since the Unix epoch. - Optionally, converts the epoch milliseconds back into a
LocalDateTimeobject.
Example:
- Input: A
LocalDateTimeobject representing2023-08-28T14:30:45. - Output: A long value representing the epoch milliseconds.
Solution Steps
- Use
ZoneOffset.UTC: Determine the time zone offset (typically UTC) to ensure the correct conversion. - Convert
LocalDateTimetoInstant: Use theatZone()method to convertLocalDateTimeto anInstant. - Get Epoch Milliseconds: Use
toEpochMilli()to obtain the epoch milliseconds. - 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 aLocalDateTimeinstance for the specified date and time.toInstant(ZoneOffset.UTC)converts theLocalDateTimeto anInstantobject, which represents an instantaneous point on the timeline.toEpochMilli()retrieves the number of milliseconds since the Unix epoch from theInstant.
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 anInstantfrom the epoch milliseconds.LocalDateTime.ofInstant(instant, ZoneOffset.UTC)converts theInstantback to aLocalDateTimeusing the UTC time zone.
Advanced Considerations
-
Time Zones: The conversion from
LocalDateTimetoInstantis 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
Instantdirectly or consider usingSystem.nanoTime(). -
Thread Safety: The
LocalDateTimeandInstantclasses 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.