Home  >  Java 8

Convert between Java LocalDateTime and Date

By Arvind Rai, August 28, 2019
This page will provide examples to convert from Java java.time.LocalDateTime to java.util.Date and from java.util.Date to java.time.LocalDateTime. The LocalDateTime, introduced in Java 8, is date-time without time-zone. The Date represents a specific instant in time, with millisecond precision.

1. LocalDateTime to Date

LocalDateTime does not consist a time-zone and Date represents a specific instant in time, with millisecond precision. To convert from LocalDateTime to Date, we need to provide time-zone.

LocalDateTime + Time-Zone = Date

We can convert LocalDateTime to Date with following methods.
1. Using LocalDateTime.atZone(): It combines this date-time with a given time-zone to create a ZonedDateTime. Then this ZonedDateTime can be converted into Instant. The method Date.from() accepts Instant and returns equivalent Date instance.
LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
System.out.println(date); 
2. Using LocalDateTime.toInstant(): It combines this local date-time and the specified offset to form an Instant.
instant = localDateTime.toInstant(ZoneOffset.UTC);
date = Date.from(instant);
System.out.println(date); 
3. Using Timestamp.valueOf(): It converts a string object in JDBC timestamp escape format to a Timestamp value. The given string date should be in yyyy-MM-dd HH:mm:ss format.
instant = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).toInstant();
date = Date.from(instant);
System.out.println(date); 
To create Date instance, we also use Date constructor by passing time in milliseconds.
Timestamp timestamp = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
date = new Date(timestamp.getTime());
System.out.println(date); 


Find the example.
LocalDateTimeToDate.java
package com.concretepage;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeToDate {
  public static void main(String[] args) {
	LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");

	Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
	Date date = Date.from(instant);
	System.out.println(date);

	instant = localDateTime.toInstant(ZoneOffset.UTC);
	date = Date.from(instant);
	System.out.println(date);

	instant = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).toInstant();
	date = Date.from(instant);
	System.out.println(date);

	Timestamp timestamp = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
	date = new Date(timestamp.getTime());
	System.out.println(date);
  }
} 
Output
Fri Nov 15 13:15:30 IST 2019
Fri Nov 15 18:45:30 IST 2019
Fri Nov 15 13:15:30 IST 2019
Fri Nov 15 13:15:30 IST 2019 

2. Date to LocalDateTime

We can convert from Date to LocalDateTime in following ways.
1. First convert Date into Instant using Instant.ofEpochMilli() method. Then use LocalDateTime.ofInstant() method to convert Instant into LocalDateTime with given zone id.
Date date = new Date();
LocalDateTime localDateTime = 
  LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
System.out.println(localDateTime); 
2. First convert Date into Instant then attach zone id using Instant.atZone() that will return ZonedDateTime. Then use ZonedDateTime.toLocalDateTime() to get LocalDateTime instance.
localDateTime = 
  Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(localDateTime); 
Find the example.

DateToLocalDateTime.java
package com.concretepage;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class DateToLocalDateTime {
  public static void main(String[] args) {
	  Date date = new Date();
	  LocalDateTime localDateTime = 
		  LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
	  System.out.println(localDateTime);
	  
	  localDateTime = 
		  Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
	  System.out.println(localDateTime);
  }
} 
Output
2019-08-28T20:31:13.473
2019-08-28T20:31:13.473 

References

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






©2019 concretepage.com | Privacy Policy | Contact Us