GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • java.time
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — java.time.OffsetDateTime Class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

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

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.