Convert between Java LocalDate and Epoch

By Arvind Rai, September 01, 2019
This page will provide examples to convert between Java LocalDate and epoch. An epoch is an instant in time used as an origin of particular calendar era. Epoch is a reference point from which a time is measured. The epoch reference point for LocalDate is 1970-01-01 in YYYY-MM-DD format. When we convert a LocalDate such as 2019-11-15 into epoch days, then the result will be number of days from 1970-01-01 to 2019-11-15. In the same way, when we convert epoch days such as 18215 to LocalDate then the resulting LocalDate will be obtained by adding 18215 days to 1970-01-01.
1. Find the code snippet to covert LocalDate to epoch days using LocalDate.toEpochDay().
long numberOfDays = localDate.toEpochDay(); 
2. Find the code snippet to covert epoch days to LocalDate using LocalDate.ofEpochDay().
LocalDate localDate = LocalDate.ofEpochDay(numberOfDays); 
Now find the examples to convert between Java LocalDate and epoch in detail.

1. LocalDate to Epoch

To convert LocalDate to epoch days is the days calculation starting from 1970-01-01 up to given local date. To convert LocalDate to epoch seconds or milliseconds is the time calculation starting from 1970-01-01T00:00:00Z up to given local date.
LocalDateToEpoch.java
package com.concretepage;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalDateToEpoch {
  public static void main(String[] args) {
	//Epoch reference of date is 1970-01-01
	LocalDate localDate = LocalDate.parse("2019-11-15");
	
	//LocalDate to epoch days
	long numberOfDays = localDate.toEpochDay();
	System.out.println(numberOfDays);
	
	//LocalDate to epoch seconds	
	long timeInSeconds = localDate.toEpochSecond(LocalTime.NOON, ZoneOffset.MIN);
	System.out.println(timeInSeconds);
	
	//LocalDate to epoch milliseconds 	
	Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();	
	long timeInMillis = instant.toEpochMilli();
	System.out.println(timeInMillis);
	
	instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
	timeInMillis = instant.toEpochMilli();
	System.out.println(timeInMillis);	
  }
} 
Output
18215
1573884000
1573756200000
1573756200000 

1.1 LocalDate to Epoch Days

LocalDate provides toEpochDay() method to convert local date into epoch days. Find the Java doc.
long toEpochDay() 
toEpochDay() converts this date to epoch Day. The toEpochDay() calculates number of days starting from 1970-01-01 up to given local date. If given local date is 1970-01-01 then count of epoch days will be 0.
LocalDate localDate = LocalDate.parse("2019-11-15"); 
long numberOfDays = localDate.toEpochDay(); 

1.2 LocalDate to Epoch Seconds

In Java 9, LocalDate provides toEpochSecond() method to convert local date into epoch seconds. Find the Java doc.
long toEpochSecond(LocalTime time, ZoneOffset offset) 
toEpochSecond() converts this LocalDate to the number of seconds since the epoch 1970-01-01T00:00:00Z. The LocalDate is combined with given time and zone offset to calculate seconds starting from 1970-01-01T00:00:00Z.
long timeInSeconds = localDate.toEpochSecond(LocalTime.NOON, ZoneOffset.MIN); 

1.3 LocalDate to Epoch Milliseconds

To convert LocalDate to epoch milliseconds, we can use Instant.toEpochMilli() that converts this instant to the number of milliseconds from the epoch 1970-01-01T00:00:00Z. To get epoch milliseconds, first we will convert LocalDate to Instant and then will use its toEpochMilli() method.
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();	
long timeInMillis = instant.toEpochMilli();

instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
timeInMillis = instant.toEpochMilli(); 

2. Epoch to LocalDate

The given epoch days, epoch seconds or epoch milliseconds are converted into LocalDate by adding the given time to 1970-01-01T00:00:00Z. Find the code.
EpochToLocalDate.java
package com.concretepage;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class EpochToLocalDate {
  public static void main(String[] args) {	
	//Epoch reference of date is 1970-01-01
	long numberOfDays = 18215;
	LocalDate localDate = LocalDate.ofEpochDay(numberOfDays);
	System.out.println(localDate);
	
	//Using Instant
	long timeInSeconds = 1567109422L;
	localDate = LocalDate.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault());	
	System.out.println(localDate);
	
	LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault());	
	localDate = localDateTime.toLocalDate();
	System.out.println(localDate);	
	
	long timeInMillis = 1567109422123L;
	localDate = LocalDate.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault());	
	System.out.println(localDate);	
	
	//Using Timestamp
	localDate = new Timestamp(timeInMillis).toLocalDateTime().toLocalDate();
	System.out.println(localDate);
  }
} 
Output
2019-11-15
2019-08-30
2019-08-30
2019-08-30
2019-08-30 

2.1 Epoch to LocalDate using LocalDate.ofEpochDay()

LocalDate.ofEpochDay() obtains an instance of LocalDate by adding days to 1970-01-01. Find the Java doc.
static LocalDate ofEpochDay(long epochDay) 
Find the code snippet.
LocalDate localDate = LocalDate.ofEpochDay(numberOfDays); 

2.2 Epoch to LocalDate using Instant

Java 9 LocalDate.ofInstant() accepts Instant and zone id and returns LocalDate object. Find the Java doc.
static LocalDate ofInstant(Instant instant, ZoneId zone) 
Instant provides following methods to handle epoch.
1. The below method obtains an instance of Instant using seconds from the epoch 1970-01-01T00:00:00Z.
static Instant ofEpochSecond(long epochSecond) 
Find the code snippet.
localDate = LocalDate.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); 
2. The below method obtains an instance of Instant using milliseconds from the epoch 1970-01-01T00:00:00Z.
static Instant ofEpochMilli(long epochMilli) 
Find the code snippet.
localDate = LocalDate.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault()); 

2.3 Epoch to LocalDate using LocalDateTime

We can convert LocalDateTime into LocalDate using LocalDateTime.toLocalDate() method.
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault());	
localDate = localDateTime.toLocalDate(); 

2.4 Epoch to LocalDate using Timestamp

Find the java.sql.Timestamp constructor.
public Timestamp(long time) 
This will construct a Timestamp object using milliseconds time value since 1970-01-01T00:00:00Z.
Find the code snippet.
localDate = new Timestamp(timeInMillis).toLocalDateTime().toLocalDate(); 

References

Class LocalDate
Class Instant
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us