Home  >  Java 8

Convert between Java LocalDateTime and Instant

By Arvind Rai, September 03, 2019
This page will provide how to convert between Java LocalDateTime and Instant. The LocalDateTime represents date-time without a time-zone such as 2019-10-25T12:15:30 whereas Instant is an instantaneous point on the time-line. We can convert between Java LocalDateTime and Instant in following ways.
1. Convert LocalDateTime to Instant using LocalDateTime.toInstant() method.
Instant instant = localDateTime.toInstant(ZoneOffset.UTC); 
2. Convert Instant to LocalDateTime using LocalDateTime.ofInstant() method.
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); 
Now find the examples to convert between Java LocalDateTime and Instant in detail.


1. LocalDateTime to Instant

Find the examples to convert LocalDateTime to Instant.
LocalDateTimeToInstant.java
package com.concretepage;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalDateTimeToInstant {
  public static void main(String[] args) {
	LocalDateTime localDateTime = LocalDateTime.parse("2019-10-25T12:15:30");
	
	//Using LocalDateTime.toInstant()
	Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
	System.out.println(instant);
	
	instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();	
	System.out.println(instant);
	
	//Using LocalDateTime.toEpochSecond() and Instant.ofEpochSecond()
	long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC);
	instant = Instant.ofEpochSecond(timeInSeconds);
	System.out.println(instant);	
  }
} 
Output
2019-10-25T12:15:30Z
2019-10-25T06:45:30Z
2019-10-25T12:15:30Z 

1.1 LocalDateTime to Instant using LocalDateTime.toInstant()

LocalDateTime.toInstant() converts this date-time to an Instant. Find the Java doc.
Instant toInstant(ZoneOffset offset) 
Find the code snippet to use it.
Instant instant = localDateTime.toInstant(ZoneOffset.UTC); 

1.2 LocalDateTime to Instant using LocalDateTime.toEpochSecond() and Instant.ofEpochSecond()

LocalDateTime.toEpochSecond() converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z. Find the Java doc.
 
long toEpochSecond(ZoneOffset offset) 
Instant.ofEpochSecond() obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z. Find the Java doc.
static Instant ofEpochSecond(long epochSecond) 
We can use LocalDateTime.toEpochSecond() and Instant.ofEpochSecond() to convert LocalDateTime to Instant in following way.
long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC);
instant = Instant.ofEpochSecond(timeInSeconds); 

2. Instant to LocalDateTime

Find the examples to convert Instant to LocalDateTime.
InstantToLocalDateTime.java
package com.concretepage;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class InstantToLocalDateTime {
  public static void main(String[] args) {	
	//Using LocalDateTime.ofInstant
	LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());	
	System.out.println(localDateTime);
	
	long timeInSeconds = 1567109422L;
	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 = Timestamp.from(Instant.now()).toLocalDateTime();
	System.out.println(localDateTime);
  }
} 
Output
2019-09-03T09:17:47.749482700
2019-08-30T01:40:22
2019-08-30T01:40:22
2019-08-30T01:40:22.123
2019-09-03T09:17:47.828487200 

2.1 Instant to LocalDateTime using LocalDateTime.ofInstant()

LocalDateTime.ofInstant() obtains an instance of LocalDateTime from an Instant and zone ID. Find the Java doc.
static LocalDateTime ofInstant(Instant instant, ZoneId zone) 
Find the code snippet to use it.
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); 

2.2 Instant to LocalDateTime using Timestamp.from()

Timestamp.from() obtains an instance of Timestamp from an Instant object. Find the Java doc.
static Timestamp from(Instant instant) 
Then use LocalDateTime.toLocalDateTime() to get LocalDateTime instance. Find the code snippet.
localDateTime = Timestamp.from(Instant.now()).toLocalDateTime(); 

References

Class LocalDateTime
Class Instant
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us