Java Convert Calendar to OffsetDateTime

Tags: Java OffsetDateTime Java Calendar Java 8

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

Table of contents

  1. How to convert Calendar to OffsetDateTime in Default System Time Zone
  2. How to convert Calendar to OffsetDateTime in UTC Time Zone

How to convert Calendar to OffsetDateTime in Default System Time Zone

In Java program, with a given Calendar object we can follow these steps to convert it to an OffsetDateTime object in system default time zone.

  • Step 1: convert Calendar Object to Date object using Calendar.getTime() method.
  • Step 2: convert Date object to Instant using Date.toInstant() method.
  • Step 3: create new OffsetDateTime from an Instant using Instant.atOffset() method with a ZoneOffset object from system default time zone.

ConvertCalendarToOffsetDateTimeExample1.java

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

public class ConvertCalendarToOffsetDateTimeExample1 {
    public static void main(String... args) {
        Calendar calendar = Calendar.getInstance();

        Date date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
        OffsetDateTime offsetDateTime = instant.atOffset(zoneOffset);

        System.out.println("Calendar: " + calendar);
        System.out.println("OffsetDateTime: " + offsetDateTime);
    }
}
The output as below.
Calendar: java.util.GregorianCalendar[time=1652519698547,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=14,DAY_OF_YEAR=134,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=14,SECOND=58,MILLISECOND=547,ZONE_OFFSET=25200000,DST_OFFSET=0]
OffsetDateTime: 2022-05-14T16:14:58.547+07:00

How to convert Calendar to OffsetDateTime in UTC Time Zone

In the following Java program we learn convert Calendar object to an OffsetDateTime object in UTC time zone.

ConvertCalendarToOffsetDateTimeExample2.java

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

public class ConvertCalendarToOffsetDateTimeExample2 {
    public static void main(String... args) {
        Calendar calendar = Calendar.getInstance();

        Date date = calendar.getTime();
        Instant instant = date.toInstant();
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.UTC);

        System.out.println("Calendar: " + calendar);
        System.out.println("OffsetDateTime: " + offsetDateTime);
    }
}
The output as below.
Calendar: java.util.GregorianCalendar[time=1652519721343,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=14,DAY_OF_YEAR=134,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=15,SECOND=21,MILLISECOND=343,ZONE_OFFSET=25200000,DST_OFFSET=0]
OffsetDateTime: 2022-05-14T09:15:21.343Z

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 Calendar

Java Convert OffsetDateTime to OffsetTime

Java Convert OffsetDateTime to String

Java Convert String to OffsetDateTime

Java Convert Date to OffsetDateTime