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

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

  • Converting ZonedDateTime to Date is very straightforward as,
    • ZoneDateTime class has toInstant() method
    • Date.from() method accepts Instant as input-argument and returns java.util.Date
  • Converted java.util.Date will have,
    • Date & Time & Zone parts same as that of ZonedDateTime
  • In short, ZonedDateTime -> Instant -> java.util.Date
  • Lets see an example for conversion of ZonedDateTime to java.util.Date in the below illustration

ConvertZonedDateTimeToJavaUtilDate.java

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

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

public class ConvertZonedDateTimeToJavaUtilDate {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" 
				+ zonedDateTime);


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


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

Output:

Zoned Date/time is :- 
2022-08-13T16:15:30.889407200+05:30[Asia/Calcutta]

Default System Zone is :- 
Asia/Calcutta

Conversion of ZonedDateTime to java.util.Date is :- 
Sat Aug 13 16:15:30 IST 2022

2. Convert java.util.Date to ZonedDateTime :

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

ConvertJavaUtilDateToZonedDateTime.java

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

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

public class ConvertJavaUtilDateToZonedDateTime {

	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 ZonedDateTime
		ZonedDateTime zonedDateTime  = date.toInstant().atZone(zoneId);
		System.out.print("\nConversion of java.util.Date to ZonedDateTime is :- \n"
				+ zonedDateTime);
	}
}

Output:

Current Date/time is :- 
Sat Aug 13 16:15:44 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of java.util.Date to ZonedDateTime is :- 
2022-08-13T16:15:44.282+05:30[Asia/Calcutta]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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