Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll demonstrate the conversion from Java’s LocalDate to Epoch and vice versa.

2. Epoch vs. LocalDate

To do the conversion, it is important to understand the concept behind Epoch and LocalDate. The ‘Epoch‘ in Java refers to the time instant of 1970-01-01T00:00:00Z. Time instants after the Epoch will have positive values. Similarly, any time instants before the Epoch will have negative values.

All instances of EpochLocalDate, and LocalDateTime are timezone-dependent, hence, when converting from one to another, we have to know the timezone. In Java, a timezone can be represented through the ZoneId class. ZoneId can be the system’s timezone obtained through the method ZoneId.systemDefault()Alternatively, ZoneId can be computed by passing a String for a known timezone such as Europe/Amsterdam.

3. Epoch to Date/Time

We can compute LocalDate or LocalDateTime given the number of milliseconds since the epoch. Alternatively, the count can be in seconds or seconds with nanoseconds adjustment. The Java data type for this count is Long. Finally, we also need to know the timezone. Let’s see how to do the conversion:

long milliSecondsSinceEpoch = 2131242L;
ZoneId zoneId = ZoneId.of("Europe/Amsterdam");
LocalDate date = Instant.ofEpochMilli(milliSecondsSinceEpoch).atZone(zoneId).toLocalDate();

In the above code snippet, we have the number of milliseconds since the Epoch in the timezone of Amsterdam, so we can use the ofEpochMilli() method of class Instant to get the LocalDate value. Otherwise, if we want to have the time instead of the date, then we’d write:

LocalDateTime time = Instant.ofEpochMilli(milliSecondsSinceEpoch).atZone(zoneId).toLocalDateTime();

In the above code snippet, we used the same approach but used the toLocalDateTime method.

4. Date/Time to Epoch

If we have a date available as LocalDate in a given timezone, then we can get the Epoch in seconds. Let’s see how:

ZoneId zoneId = ZoneId.of("Europe/Tallinn"); 
LocalDate date = LocalDate.now(); 
long EpochMilliSecondsAtDate = date.atStartOfDay(zoneId).toInstant().toEpochMilli();

In the above example, we get the Epoch seconds for today’s date and the timezone in which the system is currently in. Note that we can only get the Epoch count for the start of the day. This is because LocalDate does not have any time value information. Alternatively, if we do have the time component, we can get the exact Epoch count at a given instant:

LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
long epochMilliSecondsAtTime = localDateTime.atZone(zoneId).toInstant().toEpochMilli();

5. Conclusion

In this article, we explored how to convert from Epoch to LocalDate and LocalDateTime. We also demonstrated how to convert LocalDate or LocalDateTime to Epoch.

As always, we can find the complete code over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.