The Wayback Machine - https://web.archive.org/web/20241004181815/https://www.geeksforgeeks.org/java-time-zoneoffset-class-in-java/
Open In App

java.time.ZoneOffset Class in Java

Last Updated : 06 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A time-zone offset is that the amount of your time that a time-zone differs from Greenwich/UTC. This is often usually a hard and fast number of hours and minutes. From the time package of java, ZoneOffset class is employed to represent the fixed zone offset from UTC zone and inherits the ZoneId class and implements the Comparable interface.

public final class ZoneOffset extends ZoneId    

implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable  

ZoneOffset class has three fields:

  • MAX: It is the maximum supported zone offsets.
  • MIN: It is the minimum supported zone offsets.
  • UTC: It is the time zone offset constant for UTC.

Method of ZoneOffset class

Method Description
adjustInto(Temporal temporal) This method is used to adjusts the specified temporal object to have the same offset as this object.
compareTo(ZoneOffset other)  This method compares this offset to another offset in descending order.
equals(Object obj)  This method checks if this offset is equal to another offset.
from(TemporalAccessor temporal)  This method obtains an instance of ZoneOffset from a temporal object.
get(TemporalField field) This method is Used to get the value of the specified field from this offset as an int
getId()  This method gets the normalized zone offset ID.
getLong(TemporalField field)  This method gets the value of the specified field from this offset as a long.
getRules() This method gets the associated time-zone rules.
getTotalSeconds()  This method gets the total zone offset in seconds.
hashCode()  A hash code for this offset.
isSupported(TemporalField field) This method checks if the specified field is supported.
of(String offsetId) This method is Used to obtain an instance of ZoneOffset using the ID.
ofHours(int hours) This method is Used to obtain an instance of ZoneOffset using an offset in hours
ofHoursMinutes(int hours, int minutes) This method is Used to obtain an instance of ZoneOffset using an offset in hours and minute
ofHoursMinutesSeconds(int hours, int minutes, int seconds) This method obtains an instance of ZoneOffset using an offset in hours, minutes and seconds.
ofTotalSeconds(int totalSeconds) This method obtains an instance of ZoneOffset specifying the total offset in seconds
query(TemporalQuery<R> query) This method queries this offset using the specified query.
range(TemporalField field)  This method gets the range of valid values for the specified field.
toString()  This method outputs this offset as a String, using the normalized ID.

1. ofHoursMinutes() Method:

syntax:

public static ZoneOffset ofHoursMinutes(int Hours, int Minutes)

This method is used to obtain an instance of ZoneOffset using an offset in hours and minutes.

Java




// Example of ofHoursMinutes() Method
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

2. ofHours() Method

syntax:

public static ZoneOffset ofHours(int hours)

Obtains an instance of ZoneOffset using an offset in hours.

Java




//Example of ofHours() Method
import java.time.*;
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


Similar Reads

ZoneOffset compareTo(ZoneOffset) method in Java with Examples
The compareTo(ZoneOffset) method of ZoneOffset Class in java.time package is used to compare another instance of ZoneOffset passed as the parameter to this ZoneOffset instance. This method returns an integer value which is the comparator value. Syntax: public int compareTo(ZoneOffset anotherZoneOffset) Parameters: This method accepts accepts a para
2 min read
ZoneOffset of(String) method in Java with Examples
The of(String) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offsetId passed as the parameter. This method takes the offsetId as parameter in the form of String and converts it into the ZoneOffset. The ID of the returned offset will be normalized to one of the formats described by getId(). The
2 min read
ZoneOffset ofHours(int) method in Java with Examples
The ofHours(int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours passed as the parameter. This method takes the hours as parameter in the form of int and converts it into the ZoneOffset. The maximum supported range is from +18:00 to -18:00 inclusive. Syntax: public static ZoneOff
2 min read
ZoneOffset adjustInto(Temporal) method in Java with Examples
The adjustInto(Temporal) method of ZoneOffset Class in java.time package is used to obtain an adjusted Temporal instance with this instance of ZoneOffset adjusted into it. This method takes the Temporal instance as parameter and returns an Temporal instance, which is the adjusted instance. Syntax: public Temporal adjustInto(Temporal temporalInstanc
2 min read
ZoneOffset ofHoursMinutes(int, int) method in Java with Examples
The ofHoursMinutes(int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours and minutes passed as the parameter. This method takes the hours and minutes as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneOffset ofHoursMinutes(int hours
2 min read
ZoneOffset from(TemporalAccessor) method in Java with Examples
The from(TemporalAccessor) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in temporalAccessor passed as the parameter. This method takes the temporalAccessor as parameter in the form of TemporalAccessor and converts it into the ZoneOffset. Syntax: public static ZoneOffset from(TemporalAc
1 min read
ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
The ofHoursMinutesSeconds(int, int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours, minutes and seconds passed as the parameter. This method takes the hours, minutes and seconds as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneO
2 min read
ZoneOffset ofTotalSeconds(int) method in Java with Examples
The ofTotalSeconds(int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in totalSeconds passed as the parameter. This method takes the totalSeconds as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneOffset ofTotalSeconds(int totalSeconds) Parame
2 min read
ZoneOffset getTotalSeconds() method in Java with Examples
The getTotalSeconds() method of ZoneOffset Class in java.time package is used to obtain total number of seconds in this instance of ZoneOffset. This method does not takes any parameter and returns an int value. which is the total number of seconds. Syntax: public static int getTotalSeconds() Parameters: This method accepts do not accepts any parame
1 min read
ZoneOffset getId() method in Java with Examples
The getId() method of ZoneOffset Class in java.time package is used to obtain offset ID in this instance of ZoneOffset. This method does not takes any parameter and returns an String value. which is the offset ID. Syntax: public static String getId() Parameters: This method accepts do not accepts any parameter. Return Value: This method returns an
1 min read
ZoneOffset toString() method in Java with Examples
The toString() method of ZoneOffset Class in java.time package is used to obtain String representation of this instance of ZoneOffset. This method does not takes any parameter and returns an String value. which is the String representation. Syntax: public static String toString() Parameters: This method accepts do not accepts any parameter. Return
1 min read
ZoneOffset hashCode() method in Java with Examples
The hashCode() method of ZoneOffset Class in java.time package is used to obtain hashCode value of this instance of ZoneOffset. This method does not takes any parameter and returns an int value. which is the hashCode value. Syntax: public static int hashCode() Parameters: This method accepts do not accepts any parameter. Return Value: This method r
1 min read
ZoneOffset systemDefault() method in Java with Examples
The systemDefault() method of the ZoneOffset class in Java is used to return the system default time-zone. Syntax: public String systemDefault() Parameters: This method does not accepts any parameters. Return Value: This method returns the system default time-zone. Exceptions: This method throws following exception: DateTimeException - It throws th
1 min read
ZoneOffset ofOffset() method in Java with Examples
The ofOffset() method of the ZoneOffset class used to obtain an instance of ZoneOffset wrapping an offset.If the prefix passed in this method is "GMT", "UTC", or "UT" a ZoneOffset with the prefix then the non-zero offset is returned and If the prefix is empty "" then the ZoneOffset is returned.Syntax: public static ZoneId ofOffset(String prefix, Zo
1 min read
ZoneOffset normalized() method in Java with Examples
The normalized() method of the ZoneOffset class in Java is used to normalize the time-zone ID and returns a ZoneOffset where possible. The method returns a normalized ZoneOffset that can be used in place of this ID. The normalization process checks if the rules of this ZoneOffset have a fixed offset. If ZoneOffset has fixed offset then the ZoneOffs
1 min read
ZoneOffset getDisplayName() method in Java with Examples
The getDisplayName() method of the ZoneOffset class is used to get the textual representation of the zone suitable for presentation to the user such as 'British Time' or '+02:00'.If no textual mapping is found then the full ID is returned. Syntax: public String getDisplayName(TextStyle style, Locale locale) Parameters: This method accepts two param
2 min read
ZoneOffset getAvailableZoneIds() method in Java with Examples
The getAvailableZoneIds() method of the ZoneOffset class of java.time package is used to get the set of available zone IDs. This set includes all available region-based IDs. The ID can be passed to of(String) to create a ZoneId. The set of zone IDs can increase over time, although in a typical application the set of IDs is fixed. Syntax: public sta
2 min read
ZoneOffset query(TemporalQuery) method in Java with Examples
The query(TemporalQuery) method of ZoneOffset Class in java.time package is used to execute a query this ZoneOffset using the TemporalQuery passed as the parameter. This method returns the query result in the form of specified type. Syntax: public <R> R query(TemporalQuery<R> temporalQuery) Parameters: This method accepts a parameter Te
2 min read
ZoneOffset range(TemporalField) method in Java with Examples
ZoneOffset range(TemporalField) method in Java with Examples The range(TemporalField) method of ZoneOffset Class in java.time package is used to get the range of the temporalField, passed as the parameter, in the ZoneOffset. This method returns a ValueRange value stating the same. Syntax: public ValueRange range(TemporalField temporalField) Paramet
2 min read
ZoneOffset isSupported(TemporalField) method in Java with Examples
The isSupported(TemporalField) method of ZoneOffset Class in java.time package is used to check if the temporalField, passed as the parameter, is supported by the ZoneOffset or not. This method returns a boolean value stating the same. Syntax: public boolean isSupported(TemporalField temporalField) Parameters: This method accepts a parameter tempor
1 min read
ZoneOffset getRules() method in Java with Examples
The getRules() method of ZoneOffset Class in java.time package is used to get the associated time-zone rules of this ZoneOffset instance. This method takes returns ZoneRules of this ZoneOffset instance. Syntax: public ZoneRules getRules() Parameters: This method do not accepts any parameter. Return Value: This method returns a ZoneRules which is th
1 min read
ZoneOffset getLong(TemporalField) method in Java with Examples
The getLong(TemporalField) method of ZoneOffset Class in java.time package is used to get the value of the specified TemporalField from this ZoneOffset instance. This method takes the TemporalField as the parameter and returns an long value of this field. Syntax: public long getLong(TemporalField temporalField) Parameters: This method accepts a par
2 min read
ZoneOffset get(TemporalField) method in Java with Examples
The get(TemporalField) method of ZoneOffset Class in java.time package is used to get the value of the specified TemporalField from this ZoneOffset instance. This method takes the TemporalField as the parameter and returns an integer value of this field. Syntax: public int get(TemporalField temporalField) Parameters: This method accepts a parameter
2 min read
ZoneOffset equal(Object) method in Java with Examples
The equals(Object) method of ZoneOffset Class in java.time package is used to check whether the another instance of ZoneOffset passed as the parameter to this ZoneOffset instance, is equal or not. This method returns a boolean value stating the same. Syntax: public boolean equals(Object object) Parameters: This method accepts a parameter object whi
2 min read
Why BufferedReader class takes less time for I/O operation than Scanner class in java
In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla
3 min read
Difference between Compile-time and Run-time Polymorphism in Java
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. In this article, we will see the difference between two types of polymorphisms, compile time and run time. Compile Time Polymorphism: Whenever an object is bound with its functionality at the
3 min read
java.time.Clock Class in Java
Java Clock class is present in java.time package. It was introduced in Java 8 and provides access to current instant, date, and time using a time zone. The use of the Clock class is not mandatory because all date-time classes also have a now() method that uses the system clock in the default time zone. The main purpose of using Clock class is to pl
3 min read
java.time.Year Class in Java
The java.time.Year class represents a year in the ISO-8601 calendar system, such as 2021. Year is an immutable date-time object that represents a year. This class does not store or represent a month, day, time, or time-zone. The years represented by this class follow the proleptic numbering system that is as follows: The year 0 is preceded by year
8 min read
java.time.format.DecimalStyle Class in Java
java.time.format.DecimalStyle class acts as a central point for accessing the information. A significant part of handling dates and times is that localization. This class provides localized decimal style utilized in date and time formatting. Clone, finalize, getClass, notify, notifyAll, wait, are the methods that are declared in this class. Syntax:
2 min read
java.time.OffsetDateTime Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kind of application like mobile application, desktop application, web application. In this Java java.time.OffsetDate, Time class is imported which is an immutable representation of a date-time with an offset. This class stores all date and time f
4 min read