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

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

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

  • GregorianCalendar.from() method accepts ZonedDateTime as input-argument and returns GregorianCalendar
    • First, convert Instant to ZonedDateTime using atZone() method of Instant passing ZoneId as inputargument which will return ZonedDateTime
    • And then pass converted ZonedDateTime to GregorianCalendar.from() method which returns GregorianCalendar
  • Converted GregorianCalendar will have,
    • Date & Time & Zone/Offset parts shifted to that particular Zone
  • In short, Instant -> ZonedDateTime -> GregorianCalendar
  • Lets see an example for conversion of Instant to GregorianCalendar in the below illustration

ConvertInstantToGregorianCalendar.java

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

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

public class ConvertInstantToGregorianCalendar {

	public static void main(String[] args) {

		// 1. get instantaneous moment at UTC/GMT
		Instant instant = Instant.now();
		System.out.println("Current Instant at UTC/GMT is :- \n" 
				+ instant);


		// 2. convert Instant -> ZonedDateTime -> GregorianCalendar 
		GregorianCalendar gregorianCalendar = GregorianCalendar
				.from(instant.atZone(ZoneId.systemDefault()));


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

Output:

Current Instant at UTC/GMT is :- 
2022-08-21T05:45:26.395963100Z

Conversion of Instant to GregorianCalendar is :- 
Sun Aug 21 11:15:26 IST 2022

2. Convert GregorianCalendar to an Instant :

  • Instantiate GregorianCalendar object for conversion to an Instant
  • Conversion steps
    • First, convert GregorianCalendar to ZonedDateTime using toZonedDateTime() method
    • And then convert ZonedDateTime to an Instant using toInstant() method
  • Converted Instant will have,
    • Date & Time & Zone/Offset parts represents current Instant date/time at GMT/UTC
  • In short, GregorianCalendar -> ZonedDateTime -> Instant
  • Lets see an example for conversion of GregorianCalendar to an Instant in the below illustration

ConvertGregorianCalendarToInstant.java

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

import java.time.Instant;
import java.util.GregorianCalendar;

public class ConvertGregorianCalendarToInstant {

	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 -> Instant
		Instant instant = gregorianCalendar
				.toZonedDateTime()
				.toInstant();


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

Output:

Current Date/Time is :- 
Sun Aug 21 11:15:11 IST 2022

Conversion of GregorianCalendar to an Instant is :- 
2022-08-21T05:45:11.522Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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