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