Convert between Java LocalDateTime and Epoch

By Arvind Rai, August 30, 2019
This page will provide examples to convert between Java LocalDateTime and epoch time. 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 LocalDateTime is 1970-01-01T00:00:00Z in UTC. When we convert a local date-time such as 2019-11-15T13:15:30 into epoch seconds, then the result will be time gap in seconds from 1970-01-01T00:00:00Z to 2019-11-15T13:15:30. In the same way, when we convert epoch seconds such as 1567109422 to LocalDateTime then the resulting LocalDateTime will be obtained by adding 1567109422 seconds to 1970-01-01T00:00:00Z.
1. Find the code snippet to convert LocalDateTime to epoch seconds using LocalDateTime.toEpochSecond().
long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC); 
2. Find the code snippet to convert epoch seconds to LocalDateTime using LocalDateTime.ofEpochSecond().
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeInSeconds, 0, ZoneOffset.UTC); 
Now find the examples to convert between Java LocalDateTime and epoch time in detail.

1. LocalDateTime to Epoch

To convert LocalDateTime to epoch seconds or milliseconds is the time calculation starting from 1970-01-01T00:00:00Z up to given local date-time. Find the code.
LocalDateTimeToEpoch.java
package com.concretepage;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalDateTimeToEpoch {
  public static void main(String[] args) {
	//1970-01-01T00:00:00Z
	LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
	
	//LocalDateTime to epoch seconds
	long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC);
	System.out.println(timeInSeconds);
	
	//LocalDateTime to epoch milliseconds 	
	Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();	
	long timeInMillis = instant.toEpochMilli();
	System.out.println(timeInMillis);
  }
} 
Output
1573823730
1573803930000 

1.1 LocalDateTime to Epoch Seconds

LocalDateTime provides toEpochSecond() method to convert local date-time into epoch seconds. Find the Java doc.
long toEpochSecond(ZoneOffset offset) 
toEpochSecond() combines this local date-time and the specified offset to calculate the epoch-second value. toEpochSecond() calculates number of seconds starting from 1970-01-01T00:00:00Z up to given local date-time.
long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC); 

1.2 LocalDateTime to Epoch Milliseconds

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

2. Epoch to LocalDateTime

The given epoch seconds or milliseconds are converted into LocalDateTime by adding the given time to 1970-01-01T00:00:00Z. Find the code.
EpochToLocalDateTime.java
package com.concretepage;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class EpochToLocalDateTime {
  public static void main(String[] args) {
	//Epoch point of time is 1970-01-01T00:00:00Z
	long timeInSeconds = 1567109422L;
	
	//Using LocalDateTime.ofEpochSecond
	LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeInSeconds, 0, ZoneOffset.UTC);
	System.out.println(localDateTime);
	
	//Using Instant
	localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault());	
	System.out.println(localDateTime);
	
	localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds, 0), ZoneId.systemDefault());	
	System.out.println(localDateTime);	
	
	long timeInMillis = 1567109422123L;
	localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault());	
	System.out.println(localDateTime);	
	
	//Using Timestamp
	localDateTime = new Timestamp(timeInMillis).toLocalDateTime();
	System.out.println(localDateTime);
  }
} 
Output
2019-08-29T20:10:22
2019-08-30T01:40:22
2019-08-30T01:40:22
2019-08-30T01:40:22.123
2019-08-30T01:40:22.123 

2.1 Epoch to LocalDateTime using LocalDateTime.ofEpochSecond()

LocalDateTime.ofEpochSecond() obtains an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z. Find the Java doc.
static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) 
Find the code snippet.
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeInSeconds, 0, ZoneOffset.UTC); 

2.2 Epoch to LocalDateTime using Instant

Instant provides following methods to handle epoch time.
1. This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.
static Instant ofEpochSecond(long epochSecond) 
Find the code snippet.
localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); 
2. This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) 
Find the code snippet.
localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds, 0), ZoneId.systemDefault()); 
3. This method obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
static Instant ofEpochMilli(long epochMilli) 
Find the code snippet.
localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault()); 

2.3 Epoch to LocalDateTime 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.
localDateTime = new Timestamp(timeInMillis).toLocalDateTime(); 

References

Class LocalDateTime
Class Instant
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us