Java Convert ZonedDateTime to GregorianCalendar

Tags: Java ZonedDateTime Java GregorianCalendar Java 8

In this Java core tutorial we learn how to convert a java.time.ZonedDateTime object to a java.util.GregorianCalendar object in Java programming language.

How to convert ZonedDateTime to GregorianCalendar in Java

In Java, with a given ZonedDateTime object we can use the GregorianCalendar.from(ZonedDateTime zdt) method to instantiate a new GregorianCalendar object from ZonedDateTime value as the example Java code below.

ConvertZonedDateTimeToGregorianCalendarExample1.java

import java.time.ZonedDateTime;
import java.util.GregorianCalendar;

public class ConvertZonedDateTimeToGregorianCalendarExample1 {
    public static void main(String... args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        // Convert ZonedDateTime object to GregorianCalendar object
        GregorianCalendar gregorianCalendar = GregorianCalendar.from(zonedDateTime);

        System.out.println("ZonedDateTime: " + zonedDateTime);
        System.out.println("GregorianCalendar: " + gregorianCalendar);
    }
}
The output as below.
ZonedDateTime: 2022-05-22T18:56:35.806622900+07:00[Asia/Bangkok]
GregorianCalendar: java.util.GregorianCalendar[time=1653220595806,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=4,WEEK_OF_YEAR=20,WEEK_OF_MONTH=3,DAY_OF_MONTH=22,DAY_OF_YEAR=142,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=56,SECOND=35,MILLISECOND=806,ZONE_OFFSET=25200000,DST_OFFSET=0]

Happy Coding 😊

Java Convert GregorianCalendar to Instant

Java Convert GregorianCalendar to ZonedDateTime