Open In App

java.time.ZoneOffset Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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


Last Updated : 06 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads