Java Convert GregorianCalendar to ZonedDateTime

Tags: Java GregorianCalendar Java ZonedDateTime Java 8

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

How to convert GregorianCalendar to ZonedDateTime in Java

In Java, with a given GregorianCalendar object we can use the GregorianCalendar.toZonedDateTime() method to convert it to a ZonedDateTime object as the example Java code below.

ConvertGregorianCalendarToZonedDateTimeExample1.java

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

public class ConvertGregorianCalendarToZonedDateTimeExample1 {
    public static void main(String... args) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();

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

        System.out.println("GregorianCalendar: " + gregorianCalendar);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
GregorianCalendar: java.util.GregorianCalendar[time=1653220119241,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=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=4,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=48,SECOND=39,MILLISECOND=241,ZONE_OFFSET=25200000,DST_OFFSET=0]
ZonedDateTime: 2022-05-22T18:48:39.241+07:00[Asia/Bangkok]

Happy Coding 😊

Java Convert GregorianCalendar to Instant

Java Convert ZonedDateTime to GregorianCalendar