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

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

LocalDate 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 LocalDate to GregorianCalendar :

  • LocalDate consist of only Date information whereas GregorianCalendar requires Date, Time and Zone information
    • GregorianCalendar = LocalDate + Time + Zone information
    • For LocalDate to GregorianCalendar conversion, Time-Zone and Time information is required
  • GregorianCalendar.from() method accepts ZonedDateTime as input-argument and returns GregorianCalendar
  • Converted GregorianCalendar 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 -> GregorianCalendar
  • Lets see an example for conversion of LocalDate to GregorianCalendar in the below illustration

ConvertLocalDateToGregorianCalendar.java

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

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

public class ConvertLocalDateToGregorianCalendar {

	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 java.time.LocalDate -> java.time.ZonedDateTime -> java.util.GregorianCalendar 
		GregorianCalendar gregorianCalendar = GregorianCalendar.from(localDate.atStartOfDay(zoneId));
		System.out.print("\nConversion of LocalDate to GregorianCalendar is :- \n"
				+ gregorianCalendar.getTime());
	}
}

Output:

Current System Date is :- 
2022-08-02

Default System Zone is :- 
Asia/Calcutta

Conversion of LocalDate to GregorianCalendar is :- 
Tue Aug 02 00:00:00 IST 2022

2. Convert GregorianCalendar to LocalDate :

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

ConvertGregorianCalendarToLocalDate.java

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

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

public class ConvertGregorianCalendarToLocalDate {

	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 java.util.GregorianCalendar -> java.time.ZonedDateTime -> java.time.LocalDate
		LocalDate localDate = gregorianCalendar.toZonedDateTime().toLocalDate();
		System.out.print("\nConversion of GregorianCalendar to LocalDate is :- \n"
				+ localDate);
	}
}

Output:

Current Date/Time is :- 
Tue Aug 02 16:57:29 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of GregorianCalendar to LocalDate is :- 
2022-08-02

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?