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
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — java.time.OffsetTime 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.OffsetTime Class in Java

Last Updated : 9 Sep, 2026

The OffsetTime class in Java represents a time with a fixed offset from UTC/Greenwich, such as 18:30:45+08:00. It belongs to the java.time package and stores time values with nanosecond precision.

  • OffsetTime is immutable and thread-safe.
  • It stores the hour, minute, second, nanosecond, and zone offset.
  • The class provides methods to create, compare, modify, format, and retrieve different parts of a time.

Example: Program to create an OffsetTime object with a specific time and UTC offset:

Java
import java.time.OffsetTime;
import java.time.ZoneOffset;

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

        OffsetTime time = OffsetTime.of(18, 30, 45, 0, ZoneOffset.ofHours(8));

        System.out.println(time);
    }
}

Output
18:30:45+08:00

Explanation: The of() method creates an OffsetTime using the hour, minute, second, nanosecond, and UTC offset.

Syntax

public final class OffsetTime
implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable

Creating OffsetTime

Java provides different methods for creating an OffsetTime object. The most commonly used methods are now() and of().

1. Using now() Method

The now() method obtains the current time from the system clock. The returned OffsetTime object includes the time and the system's offset.

Java
import java.time.OffsetTime;
import java.time.temporal.ChronoField;

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

        OffsetTime offset = OffsetTime.now();

        int h = offset.get(ChronoField.HOUR_OF_DAY);
        System.out.println(h);

        int m = offset.get(ChronoField.MINUTE_OF_DAY);
        System.out.println(m);

        int s = offset.get(ChronoField.SECOND_OF_DAY);
        System.out.println(s);
    }
}

Output
9
579
34769

Explanation: The program creates an OffsetTime object using now(). It then uses ChronoField with get() to retrieve the hour, minute, and second-related fields.

2. Using getHour(), getMinute(), getSecond() and getNano() Methods

The getHour(), getMinute(), getSecond(), and getNano() methods retrieve individual components of an OffsetTime object. These methods return the corresponding time field directly.

Java
import java.time.OffsetTime;

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

        OffsetTime offset = OffsetTime.now();

        int hour = offset.getHour();
        int minute = offset.getMinute();
        int second = offset.getSecond();
        int nano = offset.getNano();

        System.out.println(hour + " hours");
        System.out.println(minute + " minutes");
        System.out.println(second + " seconds");
        System.out.println(nano + " nanoseconds");
    }
}

Output
9 hours
41 minutes
12 seconds
834486561 nanoseconds

Explanation: The program obtains the current OffsetTime and retrieves its hour, minute, second, and nanosecond components. The exact output varies according to the system clock.

Using of()

The of() method creates an OffsetTime from a LocalTime and a ZoneOffset. It is useful when a specific time needs to be associated with a particular UTC offset.

Java
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;

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

        OffsetTime time = OffsetTime.of(
                LocalTime.now(),
                ZoneOffset.UTC);

        System.out.println(time);
    }
}

Output
09:42:52.036487319Z

Explanation: The program obtains the current LocalTime and combines it with ZoneOffset.UTC. The resulting OffsetTime displays the local time together with the UTC offset.

Commonly Used Methods of OffsetTime

MethodDescription
now()Obtains the current time from the system clock.
of()Creates an OffsetTime from a local time and offset.
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 zone offset.
toLocalTime()Returns the LocalTime part of the time.
plusHours()Adds the specified number of hours.
plusMinutes()Adds the specified number of minutes.
minusHours()Subtracts the specified number of hours.
minusMinutes()Subtracts the specified number of minutes.
compareTo()Compares one OffsetTime with another.
equals()Checks whether two OffsetTime objects are equal.
format()Formats the time using a DateTimeFormatter.
parse()Parses text into an OffsetTime.
toString()Returns the time as a string.
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.