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

In this article, we will learn how to convert LocalDate to java.util.Date using atStartOfDay() method of LocalDate 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 LocalDate to java.util.Date :

  • LocalDate consist of only Date information whereas java.util.Date requires Date, Time and Zone information
    • java.util.Date = LocalDate + Time + Zone information
    • For LocalDate to java.util.Date conversion, Time-Zone and Time information is required
  • Date.from() method accepts Instant as input-argument
    1. Convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
    2. Now, pass above converted Instant from LocalDate to Date.from() method which will return java.util.Date
  • Converted java.util.Date will have,
    1. Date part same as that of LocalDate
    2. Adding System default Zone information
    3. Time part fields hour/minute/second values set to 00
  • In short, LocalDate -> ZonedDateTime -> Instant -> java.util.Date
  • Lets see an example for conversion of LocalDate to java.util.Date in the below illustration

ConvertLocalDateToJavaUtilDate.java

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

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

public class ConvertLocalDateToJavaUtilDate {

	public static void main(String[] args) {

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


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


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

Output:

Current System Date is :- 
2022-08-02

Default System Zone is :- 
Asia/Calcutta

Conversion of LocalDate to java.util.Date is :- 
Tue Aug 02 00:00:00 IST 2022

2. Convert java.util.Date to LocalDate :

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

ConvertJavaUtilDateToLocalDate.java

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

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

public class ConvertJavaUtilDateToLocalDate {

	public static void main(String[] args) {

		// 1. get current Date
		Date date = new Date();
		System.out.println("Current Date 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 LocalDate
		LocalDate localDate = date.toInstant().atZone(zoneId).toLocalDate();
		System.out.print("\nConversion of java.util.Date to LocalDate is :- \n"
				+ localDate);
	}
}

Output:

Current Date is :- 
Tue Aug 02 10:33:12 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of java.util.Date to LocalDate is :- 
2022-08-02

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?