Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?

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

LocalDateTime to GregorianCalendar conversion & vice-versa :

  • There are 3 new methods introduced in Java 1.8 version for GregorianCalendar class, those are
    1. from(ZonedDateTime) – This static method obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object
    2. toZonedDateTime() – This method converts invoking GregorianCalendar object to a ZonedDateTime that represents the same point on the time-line as that of GregorianCalendar object
    3. getCalendarType() – Returns "gregory" as the calendar type

1. Convert LocalDateTime to GregorianCalendar :

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

ConvertLocalDateTimeToGregorianCalendar.java

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

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

public class ConvertLocalDateTimeToGregorianCalendar {

	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 LocalDate -> ZonedDateTime -> GregorianCalendar 
		GregorianCalendar gregorianCalendar = GregorianCalendar
				.from(localDateTime.atZone(zoneId));


		// 4. print to console
		System.out.print("\nConversion of LocalDateTime to GregorianCalendar is :- \n"
				+ gregorianCalendar.getTime());
	}
}

Output:

Current System Date/time is :- 
2022-08-09T18:09:26.409969600

Default System Zone is :- 
Asia/Calcutta

Conversion of LocalDateTime to GregorianCalendar is :- 
Tue Aug 09 18:09:26 IST 2022

2. Convert GregorianCalendar to LocalDateTime :

  • Instantiate GregorianCalendar object for conversion to LocalDateTime
  • Conversion steps
    1. Convert GregorianCalendar to ZonedDateTime using toZonedDateTime() method
    2. And then invoke toLocalDateTime() method which returns LocalDateTime
  • In short, GregorianCalendar -> ZonedDateTime -> LocalDateTime
  • Lets see an example for conversion of GregorianCalendar to LocalDateTime in the below illustration

ConvertGregorianCalendarToLocalDateTime.java

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

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

public class ConvertGregorianCalendarToLocalDateTime {

	public static void main(String[] args) {

		// 1. Instantiate GregorianCalendar
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		System.out.println("Current Date/Time is :- \n" 
				+ gregorianCalendar.getTime());


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


		// 3. convert GregorianCalendar -> ZonedDateTime -> LocalDateTime
		LocalDateTime localDateTime  = gregorianCalendar
				.toZonedDateTime()
				.toLocalDateTime();


		// 4. print to console
		System.out.print("\nConversion of GregorianCalendar to LocalDateTime is :- \n"
				+ localDateTime);
	}
}

Output:

Current Date/Time is :- 
Tue Aug 09 18:09:44 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of GregorianCalendar to LocalDateTime is :- 
2022-08-09T18:09:44.739

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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