Java Convert ZonedDateTime to Calendar
Tags: Java ZonedDateTime Java Calendar Java 8
In this Java core tutorial we learn how to convert a java.time.ZonedDateTime object into java.util.Calendar object in Java programming language.
How to convert ZonedDateTime to Calendar in Java
In Java, we can use the GregorianCalendar.from() static method to create a new Calendar object from a given ZonedDateTime object as following Java program.
ConvertZonedDateTimeToCalendarExample1.java
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ConvertZonedDateTimeToCalendarExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
Calendar calendar = GregorianCalendar.from(zonedDateTime);
System.out.println("ZonedDateTime: " + zonedDateTime);
System.out.println("Calendar: " + calendar);
System.out.println("Year of Calendar: " + calendar.get(Calendar.YEAR));
System.out.println("Month of Calendar(January is 0):" + calendar.get(Calendar.MONTH));
System.out.println("Day of Month of Calendar:" + calendar.get(Calendar.DAY_OF_MONTH));
}
}
ZonedDateTime: 2022-05-07T00:05:42.584450600+07:00[Asia/Bangkok]
Calendar: java.util.GregorianCalendar[time=1651856742584,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=18,WEEK_OF_MONTH=1,DAY_OF_MONTH=7,DAY_OF_YEAR=127,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=5,SECOND=42,MILLISECOND=584,ZONE_OFFSET=25200000,DST_OFFSET=0]
Year of Calendar: 2022
Month of Calendar(January is 0):4
Day of Month of Calendar:7
Happy Coding 😊
Related Articles
Java Convert ZonedDateTime to Instant
Java Convert ZonedDateTime to Date
Java Convert ZonedDateTime to LocalDateTime
Java Convert ZonedDateTime to OffsetDateTime
Java Convert ZonedDateTime to LocalTime
Java Convert ZonedDateTime to LocalDate
Java Convert ZonedDateTime to ZoneOffset
Java Convert ZonedDateTime to ZoneId
Java Convert ZonedDateTime to Epoch Seconds