The ZoneOffset class in Java represents a fixed time-zone offset from UTC or Greenwich. An offset is expressed as a number of hours, minutes, and seconds, such as +05:30 or -04:00.
ZoneOffsetbelongs to thejava.timepackage and extendsZoneId.- It provides methods to create, compare, retrieve, and modify zone offsets.
Example: Create a ZoneOffset using hours and minutes.
import java.time.ZoneOffset;
public class GFG {
public static void main(String[] args) {
ZoneOffset zone = ZoneOffset.ofHoursMinutes(7, 15);
System.out.println(zone);
}
}
Output
+07:15
Explanation: The program uses ofHoursMinutes() to create a ZoneOffset with an offset of 7 hours and 15 minutes.
Syntax
public final class ZoneOffset extends ZoneId
implements TemporalAccessor, TemporalAdjuster,
Comparable<ZoneOffset>, Serializable
ZoneOffset class provides constants such as MAX, MIN, and UTC.
- MAX : Represents the maximum supported zone offset.
- MIN : Represents the minimum supported zone offset.
- UTC : Represents the UTC zone offset.
Creating ZoneOffset
Java provides several methods to create a ZoneOffset object. The commonly used methods include ofHours() and ofHoursMinutes().
1. Using ofHours()
The ofHours() method creates a ZoneOffset using the specified number of hours.
Syntax
public static ZoneOffset ofHours(int hours)
import java.time.ZoneOffset;
public class GFG {
public static void main(String[] args) {
ZoneOffset zone = ZoneOffset.ofHours(4);
System.out.println(zone);
}
}
Output
+04:00
Explanation: The program creates a ZoneOffset with a four-hour offset from UTC using ofHours().
2. Using ofHoursMinutes()
The ofHoursMinutes() method creates a ZoneOffset using the specified hours and minutes.
Syntax
public static ZoneOffset ofHoursMinutes(int hours, int minutes)
import java.time.ZoneOffset;
public class GFG {
public static void main(String[] args) {
ZoneOffset zone = ZoneOffset.ofHoursMinutes(7, 15);
System.out.println(zone);
}
}
Output
+07:15
Explanation: The program creates a ZoneOffset with an offset of 7 hours and 15 minutes from UTC.
Commonly Used Methods of ZoneOffset
| Method | Description |
|---|---|
of() | Creates a ZoneOffset from an offset ID. |
| ofHours() | Creates a ZoneOffset using hours. |
ofHoursMinutes() | Creates a ZoneOffset using hours and minutes. |
ofHoursMinutesSeconds() | Creates a ZoneOffset using hours, minutes, and seconds. |
ofTotalSeconds() | Creates a ZoneOffset using the total number of seconds. |
from() | Obtains a ZoneOffset from a temporal object. |
getId() | Returns the normalized offset ID. |
getTotalSeconds() | Returns the total offset in seconds. |
get() | Returns the value of the specified field as an int. |
getLong() | Returns the value of the specified field as a long. |
getRules() | Returns the time-zone rules associated with the offset. |
isSupported() | Checks whether a specified field is supported. |
adjustInto() | Adjusts a temporal object to use this offset. |
compareTo() | Compares this offset with another offset. |
equals() | Checks whether two offsets are equal. |
hashCode() | Returns the hash code of the offset. |
toString() | Returns the offset as a string. |
Note: ZoneOffset represents a fixed offset from UTC, such as +05:30. ZoneId represents a time-zone region, such as Asia/Kolkata, whose offset may change according to time-zone rules.