Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?

In this article, we will learn how to convert ZonedDateTime to Calendar using toInstant() method of ZonedDateTime provided in Java 1.8 version and vice-versa

ZonedDateTime to Calendar conversion & vice-versa :

Direct conversion from ZonedDateTime to java.util.Calendar isn’t possible and hence convert ZonedDateTime to java.util.Date and then to Calendar using getInstance() & setTime() methods

  • 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 Calendar :

  • Converting ZonedDateTime to Calendar is very straightforward as,
    • ZoneDateTime class has toInstant() method
    • Date.from() method accepts Instant as input-argument and returns java.util.Date
    • For converting java.util.Date to Calendar, get an instance of Calendar using getInstance() method and then invoke setTime() method passing above converted Date object as argument
  • Converted Calendar will have,
    1. Date & Time & Zone parts remain same as that of LocalDateTime
  • In short, ZonedDateTime -> Instant -> java.util.Date -> Calendar
  • Lets see an example for conversion of ZonedDateTime to Calendar in the below illustration

ConvertZonedDateTimeToJavaUtilCalendar.java

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

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

public class ConvertZonedDateTimeToJavaUtilCalendar {

	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 LocalDate to java.util.Date
		Date date = Date.from(zonedDateTime.toInstant());


		// 4. convert java.util.Date to java.util.Calendar
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		System.out.print("\nConversion of ZonedDateTime to java.util.Calendar is :- \n"
				+ date);
	}
}

Output:

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

Default System Zone is :- 
Asia/Calcutta

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

2. Convert Calendar to ZonedDateTime :

  • Get Calendar object using getInstance() method of Calendar for conversion to ZonedDateTime
  • Conversion steps
    1. Convert Calendar to Date using getTime() method
    2. And then invoke toInstant() method for Date to Instant conversion
    3. And then invoke atZone() method passing ZoneId as argument for Instant to ZonedDateTime conversion
  • In short, Calendar -> java.util.Date -> Instant -> ZonedDateTime
  • Lets see an example for conversion of Calendar to ZonedDateTime in the below illustration

ConvertJavaUtilCalendarToZonedDateTime.java

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

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

public class ConvertJavaUtilCalendarToZonedDateTime {

	public static void main(String[] args) {

		// 1. get current calendar Date
		Calendar calendar = Calendar.getInstance();
		System.out.println("Current Date is :- \n" 
				+ calendar.getTime());


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


		// 3. convert java.util.Calendar -> Date -> Instant -> ZonedDateTime
		Date date = calendar.getTime();
		ZonedDateTime localDate = date.toInstant().atZone(zoneId);
		System.out.print("\nConversion of Calendar to ZonedDateTime is :- \n"
				+ localDate);
	}
}

Output:

Current Date is :- 
Sat Aug 13 16:47:46 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of Calendar to ZonedDateTime is :- 
2022-08-13T16:47:46.609+05:30[Asia/Calcutta]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?