Sunday, September 2, 2018

Learn java.time.ZoneOffset tutorials with examples

java8 ZoneOffset class

java.time.ZoneOffset tutorials with examples
ZoneOffset class is declared in java.time package and introduced in java8  as part of Date time API enhancements. It is used to represents offset from UTC Timezone, Offset returns hours/ minutes from which UTC timezone is a difference with
timezone is part of the earth like a group or single countries on which all follow the standard time.
Each timezone contains two properties.
  • id -  Asia/Calcutta 
  • offset - time difference or offset from UTC/Greenwich - Example is +05:30


  • It is Immutable, Once Instance is created, can not able to modify its state  of the instance
  • It is Threadsafe  implemented during multi-thread environment
  • It is based on value-based class, When this classed checked against duplicate or identity instance using === operator, gives inconsistent result and behavior is specific to JVM.

Class Signature

public final class ZoneOffset
        extends ZoneId
        implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable 
It extends ZoneId class, ZoneId class represents timezone id like Europe/Paris.

Example Usage

It has of(), ofHours(),ofHoursMinutes(),ofHoursMinutesSeconds() method for creating ZoneOffset Instance.
ZoneOffset zonedOffsetUtc = ZoneOffset.UTC;
ZoneOffset zonedOffsetMin = ZoneOffset.MIN;
ZoneOffset zonedOffsetMax = ZoneOffset.MAX;
ZoneOffset zoneOffSet = ZoneOffset.ofHours(5);
Output is
Z represents timezone offset from UTC timezone How to Convert ZoneId to ZoneOffset When dealing with timezones conversions, Instance is a correct instance for conversions as this gives correct information.

Instant instantObject =; 
ZoneId systemDefaultZone = ZoneId.systemDefault(); 
ZoneOffset myzoneoffset = systemDefaultZone.getRules().getOffset(instantObject);

How to Convert ZoneOffset to ZoneId 

java.time.ZoneId class has ofOffset method which creates ZoneId from zoneOffset and prefix Syntax
public static ZoneId ofOffset(String prefix,   ZoneOffset offset)
The following code is an usage of ofOffset method
ZoneId zoneId = ZoneId.ofOffset("GMT",ZoneOffset.UTC);
System.out.println(zoneId); //GMT

How to get offset value from UTC timezone

To get offset value, first, you need to create an Instance instance than LocalDateTime. LocalDateTime will give only best available offset To get My Local System Offset, You have to use ZoneId.systemDefault()- This gives my system default timezone(ZoneId.systemDefault()) Next is to get timezone rules on which get the timezone offset using current offset( This gives the number of hours/minutes offset from UTC timezone difference
System.out.println(ZoneId.systemDefault().getRules().getOffset(; // +05:30


Method Description
adjustInto(Temporal temporal) adjust the Offset of the caller with the same offset of the parameter object
getId() Return the offset id ie Z
get(TemporalField field) Return the field from an offset as Int value
getLong() Return the field from an offset as a Long value
of(String offsetId) It is used to create a ZoneOffset object with id
ofHours(int hours) It is used to create a ZoneOffset object with offset hours
ofHoursMinutes(int offsetHours,int offsetMinutes) It is used to create a ZoneOffset object with offset hours and minutes
ofHoursMinutesSeconds(int hours, int minutes, int seconds) It is used to create a ZoneOffset object with offset hours, minutes and seconds
ofTotalSeconds(int totalSeconds) It is used to create a ZoneOffset object with total seconds
range(TemporalField field) It is used to the range of values using Temporal field

Related article