java.time.OffsetDateTime Class in Java

Last Updated : 9 Sep, 2026

The OffsetDateTime class in Java represents a date and time with a fixed offset from UTC/Greenwich. It stores date and time fields with nanosecond precision along with the offset.

  • OffsetDateTime belongs to the java.time package and is immutable and thread-safe.
  • It provides methods to retrieve date and time fields such as year, month, day, hour, minute, second, and nanosecond.
  • It also provides methods for comparing, formatting, adjusting, and performing date-time calculations.

Example: Program uses OffsetDateTime.now() to display the current date, time, and individual date-time fields.

Java
import java.time.OffsetDateTime;

public class GFG{
    public static void main(String[] args){

        OffsetDateTime dateTime = OffsetDateTime.now();

        System.out.println("Date and Time: " + dateTime);
        System.out.println("Year: " + dateTime.getYear());
        System.out.println("Month: " + dateTime.getMonth());
        System.out.println("Day: " + dateTime.getDayOfWeek());
        System.out.println("Hour: " + dateTime.getHour());
        System.out.println("Minute: " + dateTime.getMinute());
        System.out.println("Second: " + dateTime.getSecond());
    }
}

Output
Date and Time: 2026-09-08T04:20:55.125272274Z
Year: 2026
Month: SEPTEMBER
Day: TUESDAY
Hour: 4
Minute: 20
Second: 55

Explanation: The program creates an OffsetDateTime object using the system clock and then uses methods such as getYear(), getMonth(), getDayOfWeek(), getHour(), getMinute(), and getSecond() to retrieve the corresponding values.

Syntax

public final class OffsetDateTime extends Object implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable

Methods to Get Date and Time Fields from OffsetDateTime

The OffsetDateTime class provides individual getter methods for accessing different parts of a date-time. The following examples demonstrate retrieving date-related and time-related fields from the current date-time.

1. Get Year, Month and Day of Week

The getYear(), getMonth(), and getDayOfWeek() methods retrieve the year, month, and day of the week from an OffsetDateTime object. The now() method first obtains the current date and time from the system clock.

Java
import java.time.OffsetDateTime;

public class GFG{

    public static void main(String[] args)
    {

        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        System.out.println(offsetDateTime);

        System.out.println("Year  : " + offsetDateTime.getYear()
            + " | Month : " + offsetDateTime.getMonth()
            + " | Day : " + offsetDateTime.getDayOfWeek());
    }
}

Output
2026-09-08T04:04:53.152921914Z
Year  : 2026 | Month : SEPTEMBER | Day : TUESDAY

Explanation: The program creates an OffsetDateTime object using now(). It then uses getYear(), getMonth(), and getDayOfWeek() to retrieve the year, month, and day of the week. The actual output depends on the system date and time.

2. Get Hour, Minute, Second and Nanosecond

The getHour(), getMinute(), getSecond(), and getNano() methods retrieve individual time fields from an OffsetDateTime object. These methods provide the time components separately.

Java
import java.time.OffsetDateTime;

public class GFG {

    public static void main(String[] args) {

        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        System.out.println(offsetDateTime);

        System.out.println(
            "Hour : " + offsetDateTime.getHour()
            + " | Minute : " + offsetDateTime.getMinute()
            + " | Second : " + offsetDateTime.getSecond()
            + " | NanoSecond : " + offsetDateTime.getNano()
        );
    }
}

Output
2026-09-08T04:06:05.835923202Z
Hour : 4 | Minute : 6 | Second : 5 | NanoSecond : 835923202

Explanation: The program obtains the current OffsetDateTime and retrieves its hour, minute, second, and nanosecond values. The exact values vary according to the system clock.

Commonly Used Methods of OffsetDateTime

MethodDescription
now()Obtains the current date and time from the system clock.
getYear()Returns the year.
getMonth()Returns the month as a Month enum.
getDayOfWeek()Returns the day of the week.
getHour()Returns the hour of the day.
getMinute()Returns the minute of the hour.
getSecond()Returns the second of the minute.
getNano()Returns the nanosecond of the second.
getOffset()Returns the offset from UTC.
toLocalDate()Returns the LocalDate part of the date-time.
toLocalTime()Returns the LocalTime part of the date-time.
format()Formats the date-time using a DateTimeFormatter.
compareTo()Compares one OffsetDateTime with another.
equals()Checks whether two OffsetDateTime objects are equal.
isAfter()Checks whether the instant occurs after another date-time.
isBefore()Checks whether the instant occurs before another date-time.
plusDays()Adds the specified number of days.
plusHours()Adds the specified number of hours.
plusMinutes()Adds the specified number of minutes.
minusDays()Subtracts the specified number of days.
minusHours()Subtracts the specified number of hours.
minusMinutes()Subtracts the specified number of minutes.
range()Returns the valid range for a specified temporal field.
atZoneSameInstant()Combines the date-time with a time zone while preserving the same instant.
atZoneSimilarLocal()Combines the date-time with a time zone while trying to preserve the local date and time.
Comment