Java ZoneOffset

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java ZoneOffset class

Java ZoneOffset class is used to represent the fixed zone offset from UTC time zone. It inherits the ZoneId class and implements the Comparable interface.

The ZoneOffset class declares three constants:

  • 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.


Java ZoneOffset class declaration

Let's see the declaration of java.time.ZoneOffset class.

  1. public final class ZoneOffset extends ZoneId   

  2. implements TemporalAccessor, TemporalAdjuster, Comparable, Serializable

Methods of Java ZoneOffset

Method

Description

Temporal adjustInto(Temporal temporal)

It is used to adjust the specified temporal object to have the same offset as this object.

int get(TemporalField field)

It is used to get the value of the specified field from this offset as an int.

boolean isSupported(TemporalField field)

It is used to check if the specified field is supported.

static ZoneOffset of(String offsetId)

It is used to obtain an instance of ZoneOffset using the ID.

static ZoneOffset ofHours(int hours)

It is used to obtain an instance of ZoneOffset using an offset in hours.

static ZoneOffset ofHoursMinutes(int hours, int minutes)

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

ValueRange range(TemporalField field)

It is used to get the range of valid values

Java ZoneOffset Example

  1. import java.time.*;  
  2. import java.time.temporal.Temporal;  
  3. public class ZoneOffsetExample {  
  4.   public static void main(String[] args) {  
  5.     ZoneOffset zone = ZoneOffset.UTC;  
  6.     Temporal temp = zone.adjustInto(ZonedDateTime.now());  
  7.     System.out.println(temp);  
  8.   }  
  9. }  

Output:

2017-01-29T12:43:00.702+05:30[Asia/Calcutta]

Java ZoneOffset Example: ofHours()

  1. import java.time.ZoneOffset;  
  2. public class ZoneOffsetExample {  
  3.   public static void main(String[] args) {  
  4.     ZoneOffset zone = ZoneOffset.ofHours(5);  
  5.     System.out.println(zone);  
  6.   }  
  7. }  

Output:

+05:00

Java ZoneOffset Example: ofHoursMinutes()

  1. import java.time.ZoneOffset;  
  2. public class ZoneOffsetExample {  
  3.   public static void main(String[] args) {  
  4.     ZoneOffset zone = ZoneOffset.ofHoursMinutes(5,30);  
  5.     System.out.println(zone);  
  6.   }  
  7. }  

Output:

+05:30

Java ZoneOffset Example: isSupported()

  1. import java.time.ZoneOffset;  
  2. import java.time.temporal.ChronoField;  
  3. public class ZoneOffsetExample {  
  4.   public static void main(String[] args) {  
  5.     ZoneOffset zone = ZoneOffset.UTC;  
  6.     boolean b1 = zone.isSupported(ChronoField.OFFSET_SECONDS);  
  7.     System.out.println(b1);  
  8.     boolean b2 = zone.isSupported(ChronoField.SECOND_OF_MINUTE);  
  9.     System.out.println(b2);  
  10.   }  
  11. }  

Output:

true
false