Java Convert OffsetDateTime to Calendar

Tags: Java OffsetDateTime Java Calendar Java 8

In this Java core tutorial we learn how to convert an OffsetDateTime object to a Calendar object in Java programming language.

How to convert OffsetDateTime to Calendar in Java

In Java, with a given OffsetDateTime object we can convert it to a Calendar object with steps below.

  • First step, convert OffsetDateTime object to an Instant object
  • Second step, instantiate new Date object from an Instant.
  • Third step, create new instance of Calendar and set date value to it.

ConvertOffsetDateTimeToCalendarExample1.java

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.Date;

public class ConvertOffsetDateTimeToCalendarExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        Instant instant = offsetDateTime.toInstant();
        Date date = Date.from(instant);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        System.out.println("OffsetDateTime: " + offsetDateTime);
        System.out.println("Calendar: " + calendar);
    }
}
The output as below.
OffsetDateTime: 2022-05-12T23:08:06.283748100+07:00
Calendar: java.util.GregorianCalendar[time=1652371686283,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=20,WEEK_OF_MONTH=2,DAY_OF_MONTH=12,DAY_OF_YEAR=132,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=8,SECOND=6,MILLISECOND=283,ZONE_OFFSET=25200000,DST_OFFSET=0]

Happy Coding 😊

Java Convert OffsetDateTime to Epoch Seconds

Java Convert OffsetDateTime to ZoneOffset

Java Convert OffsetDateTime to LocalDate

Java Convert OffsetDateTime to LocalTime

Java Convert OffsetDateTime to LocalDateTime

Java Convert OffsetDateTime to ZonedDateTime

Java Convert OffsetDateTime to Instant

Java Convert OffsetDateTime to Date

Java Convert OffsetDateTime to OffsetTime

Java Convert OffsetDateTime to String

Java Convert String to OffsetDateTime

Java Convert Date to OffsetDateTime

Java Convert Calendar to OffsetDateTime