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

In this article, we will learn how to convert OffsetDateTime to GregorianCalendar in Java 1.8 version and vice-versa

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

  • GregorianCalendar.from() method accepts ZonedDateTime as input-argument and returns GregorianCalendar
  • First, convert OffsetDateTime to ZonedDateTime using toZonedDateTime() method of OffsetDateTime and pass converted ZonedDateTime to GregorianCalendar.from() method which will return GregorianCalendar
  • Converted GregorianCalendar will have,
    • Date & Time & Zone/Offset parts remain same as that of OffsetDateTime
  • In short, OffsetDateTime -> ZonedDateTime -> GregorianCalendar
  • Lets see an example for conversion of OffsetDateTime to GregorianCalendar in the below illustration

ConvertOffsetDateTimeToGregorianCalendar.java

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

import java.time.OffsetDateTime;
import java.util.GregorianCalendar;

public class ConvertOffsetDateTimeToGregorianCalendar {

	public static void main(String[] args) {

		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Offset Date/time is :- \n" 
				+ offsetDateTime);


		// 2. convert OffsetDateTime -> ZonedDateTime -> GregorianCalendar 
		GregorianCalendar gregorianCalendar = GregorianCalendar
				.from(offsetDateTime.toZonedDateTime());


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

Output:

Offset Date/time is :- 
2022-08-17T13:38:11.195774500+05:30

Conversion of OffsetDateTime to GregorianCalendar is :- 
Wed Aug 17 13:38:11 IST 2022

2. Convert GregorianCalendar to OffsetDateTime :

  • Instantiate GregorianCalendar object for conversion to OffsetDateTime
  • Conversion steps
    • First, convert GregorianCalendar to ZonedDateTime using toZonedDateTime() method
    • And then convert ZonedDateTime to OffsetDateTime using toOffsetDateTime() method
  • In short, GregorianCalendar -> ZonedDateTime -> OffsetDateTime
  • Lets see an example for conversion of GregorianCalendar to toOffsetDateTime in the below illustration

ConvertGregorianCalendarToOffsetDateTime.java

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

import java.time.OffsetDateTime;
import java.util.GregorianCalendar;

public class ConvertGregorianCalendarToOffsetDateTime {

	public static void main(String[] args) {

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


		// 2. convert GregorianCalendar -> ZonedDateTime -> OffsetDateTime
		OffsetDateTime OffsetDateTime  = gregorianCalendar
				.toZonedDateTime()
				.toOffsetDateTime();


		// 3. print to console
		System.out.print("\nConversion of GregorianCalendar to an OffsetDateTime is :- \n"
				+ OffsetDateTime);
	}
}

Output:

Current Date/Time is :- 
Wed Aug 17 13:40:17 IST 2022

Conversion of GregorianCalendar to an OffsetDateTime is :- 
2022-08-17T13:40:17.827+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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