Skip to content
Related Articles

Related Articles

Improve Article
java.time.ZoneOffset Class in Java
  • Last Updated : 03 Mar, 2021

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 fileds:

  • 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



MethodDescription
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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :