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
- 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 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:
- Java 8 – OffsetDateTime with method details and examples
- Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ?
- Java 8 – How to form OffsetDateTime passing Date, Time and Offset fields ?
- Java 8 – How to form OffsetDateTime passing LocalDate, LocalTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing LocalDateTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing Instant and ZoneId ?
- Java 8 – How to parse OffsetDateTime in String form ?
- Java 8 – How to convert String to OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert OffsetDateTime in different formats ?
- Java 8 – How to convert OffsetDateTime in different Format Style ?
- Java 8 – How to convert OffsetDateTime to LocalDateTime ?
- Java 8 – How to convert OffsetDateTime to ZonedDateTime ?
- Java 8 – How to convert OffsetDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from OffsetDateTime ?
- Java 8 – How to extract OffsetTime from OffsetDateTime ?
- Java 8 – How to convert OffsetDateTime to number of Seconds ?
- Java 8 – How to convert OffsetDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert OffsetDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert OffsetDateTime to Calendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an OffsetDateTime in different ways ?
- Java 8 – How to add Date and Time fields to OffsetDateTime ?
- Java 8 – How to subtract Date and Time fields from OffsetDateTime ?
- Java 8 – How to alter Date, Time and Offset fields of OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is Before another OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is After another OffsetDateTime ?
- Java 8 – How to compare two OffsetDateTime instances ?
- Java 8 – How to find difference between two OffsetDateTime using Period & Duration ?
- Java 9 – Find difference between two OffsetDateTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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/7/docs/api/java/util/GregorianCalendar.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 !!