Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?

In this article, we will learn how to convert LocalDateTime to java.util.Date using atZone() method of LocalDateTime provided in Java 1.8 version and vice-versa

Date class in Java 1.8 version :

  • There are 2 new methods introduced in Java 1.8 version for Date class, those are
    1. from(Instant) – This static method obtains an instance of Date from an Instant object
    2. toInstant() – This method converts invoking Date object to an Instant
  • Note: Many legacy methods of Date class are deprecated

1. Convert LocalDateTime to java.util.Date :

  • LocalDateTime consists of only Date & Time information but doesn’t have Zone information whereas java.util.Date requires all 3 Date, Time and Zone informations
    • java.util.Date = LocalDateTime + Zone information
    • For LocalDateTime to java.util.Date conversion, Time-Zone information is required
  • Date.from() method accepts Instant as input-argument
    1. Convert LocalDateTime to ZonedDateTime using atZone() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
    2. Now, pass above converted Instant from LocalDateTime to Date.from() method which will return java.util.Date
  • Converted java.util.Date will have,
    1. Date & Time parts are same as that of LocalDateTime
    2. Adding System default Zone information
  • In short, LocalDateTime -> ZonedDateTime -> Instant -> java.util.Date
  • Lets see an example for conversion of LocalDateTime to java.util.Date in the below illustration

ConvertLocalDateTimeToJavaUtilDate.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class ConvertLocalDateTimeToJavaUtilDate {

	public static void main(String[] args) {

		// 1. get current System Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current System Date/time is :- \n" 
				+ localDateTime);


		// 2. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" 
				+ zoneId);


		// 3. convert LocalDateTime to java.util.Date using atZone(ZoneId)
		Date date = Date.from(localDateTime.atZone(zoneId).toInstant());
		System.out.print("\nConversion of localDateTime to java.util.Date is :- \n"
				+ date);
	}
}

Output:

Current System Date/time is :- 
2022-08-09T14:42:36.482539400

Default System Zone is :- 
Asia/Calcutta

Conversion of localDateTime to java.util.Date is :- 
Tue Aug 09 14:42:36 IST 2022

2. Convert java.util.Date to LocalDateTime :

  • Get java.util.Date instantiating Date object for conversion to LocalDateTime
  • Conversion steps
    1. Convert Date to Instant using toInstant() method
    2. And then invoke atZone() method passing ZoneId as argument which returns ZonedDateTime
    3. And then invoke toLocalDateTime() method which returns LocalDateTime
  • In short, java.util.Date -> Instant -> ZonedDateTime -> LocalDateTime
  • Read Java 8 – How to convert java.util.Date to LocalDateTime in different ways ? for more details and examples
  • Lets see an example for conversion of java.util.Date to LocalDateTime in the below illustration

ConvertJavaUtilDateToLocalDateTime.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class ConvertJavaUtilDateToLocalDateTime {

	public static void main(String[] args) {

		// 1. get current Date/time
		Date date = new Date();
		System.out.println("Current Date/time is :- \n" 
				+ date);


		// 2. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" 
				+ zoneId);


		// 3. convert java.util.Date to LocalDateTime
		LocalDateTime localDateTime = date.toInstant().atZone(zoneId).toLocalDateTime();
		System.out.print("\nConversion of java.util.Date to LocalDateTime is :- \n"
				+ localDateTime);
	}
}

Output:

Current Date/time is :- 
Tue Aug 09 14:42:49 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of java.util.Date to LocalDateTime is :- 
2022-08-09T14:42:49.650

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
Java 8 – How to extract LocalDate and LocalTime from LocalDateTime ?