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
- from(ZonedDateTime) – This static method obtains an instance of
GregorianCalendar
with the default locale from aZonedDateTime
object - toZonedDateTime() – This method converts invoking GregorianCalendar object to a
ZonedDateTime
that represents the same point on the time-line as that ofGregorianCalendar
object - getCalendarType() – Returns
"gregory"
as the calendar type
- from(ZonedDateTime) – This static method obtains an instance of
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 input–argument 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:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!