Explain Codes LogoExplain Codes Logo

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

java
date-and-time
java-8
localdate
Nikita BarsukovbyNikita Barsukov·Oct 23, 2024
TLDR

To convert Epoch milliseconds to LocalDate using Java 8, you just need to follow these steps:

  1. Use Instant.ofEpochMilli() to convert the Epoch milliseconds into an Instant.
  2. Call atZone() on the Instant to assign it a time zone.
  3. Finally, call toLocalDate() to extract just the date without any time component. Here is the compact code for this:
LocalDate date = Instant.ofEpochMilli(epochMillis).atZone(ZoneId.systemDefault()).toLocalDate();

By employing this, you can create a LocalDate object that signifies the date obtained from your system's default time zone using the provided Epoch time.

Detailed explanation and tips

While working with dates and times in Java, one should not overlook the effects of time zones and must take into account the minute differences between the various date-time representations.

Handling time zones

By invoking ZoneId.systemDefault(), we're drawing upon the system's default time zone. Nonetheless, it's important to consider that:

  • The default time zone can undergo changes, resulting in inconsistencies depending on system settings.
  • For consistency irrespective of systems or historical dates, it's advisable to explicitly specify the time zone.

Advantages of LocalDateTime

While LocalDate is sufficient for representing a date without a time component, LocalDateTime encompasses both date and time without a time zone. In situations where the time segment is as crucial as the date, stick with LocalDateTime.

LocalDate vs java.util.Date

Don't confuse LocalDate with java.util.Date. The former is part of the simplified Java Date and Time API available from Java 8, which deals with dates without time or timezone awareness. In contrast, java.util.Date is part of the older, bulkier date-time API that accounts for the millisecond precision, including time zones.

Days since the Epoch calculation

In case you're interested in calculating the number of days since the Unix Epoch, utilizing Duration.ofMillis(epochMillis).toDays() would be the way to go. But remember, it returns a duration and not a LocalDate.

Converting from Timestamp

Should you have a Timestamp object and wish to convert it to LocalDateTime, the following code comes to the rescue:

LocalDateTime dateTime = new Timestamp(epochMillis).toLocalDateTime();

This method efficiently converts Epoch milliseconds to LocalDateTime.

Further Instruction and Precautions

Time-Zone Aware Conversion

Attain absolute control over the time zone by replacing the system's default with a specified ZoneId:

LocalDate date = Instant.ofEpochMilli(epochMillis).atZone(ZoneId.of("UTC")).toLocalDate();

Long Epoch Conversion Without Time Zones

For a simple conversion from Epoch milliseconds directly to LocalDate, without any regard to time zones, you can perform a division operation on the Epoch milliseconds:

LocalDate epochDay = LocalDate.ofEpochDay(TimeUnit.MILLISECONDS.toDays(epochMillis));

Remember that this will obscure any time zone data.

Daylight Saving Time and Leap Seconds

Keep a watchful eye out for daylight saving time adjustments and anomalies like leap seconds. The inclusion of ZoneId aids in navigating these quirks aligned with the local time zone's context.