java8 java.time.ZoneOffset tutorials with examples

What is ZoneOffset class in java8?

ZoneOffset class is defined in the java.time package and introduced in java8 as part of Date time API enhancements.

It is used to represent offset from UTC Time zone and Offset returns hours/ minutes difference from UTC timezone.

timezone is part of the earth like a group or single countries on which all follow the standard time.

Each time zone contains two properties.

  • id -Asia/Calcutta
  • offset - time difference or offset from UTC/Greenwich - Example is +05:30

ZoneOffset features:

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

Class Signature:

public final class ZoneOffset
        extends ZoneId
        implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable

It extends the ZoneId class, and this class represents time zone id like Europe/Paris.

Example Usage

It has of(), ofHours(),ofHoursMinutes(),ofHoursMinutesSeconds() method for creating ZoneOffset Instance.

import java.time.ZoneOffset;
public class Test {
    public static void main(String[] args) {
        ZoneOffset zonedOffsetUtc = ZoneOffset.UTC;
        ZoneOffset zonedOffsetMin = ZoneOffset.MIN;
        ZoneOffset zonedOffsetMax = ZoneOffset.MAX;
        System.out.println(zonedOffsetUtc);
        System.out.println(zonedOffsetMin);
        System.out.println(zonedOffsetMax);
        ZoneOffset zoneOffSet = ZoneOffset.ofHours(5);
        System.out.println(zoneOffSet);
    }
}

Output:

Z
-18:00
+18:00
+05:00

Z represents a time zone offset from UTC.

How to Convert ZoneId to ZoneOffset in java8?

When dealing with timezones conversions, Instance is a correct instance for conversions as this gives correct information.

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Test {
    public static void main(String[] args) {
        Instant instantObject = Instant.now();
        ZoneId systemDefaultZone = ZoneId.systemDefault();
        ZoneOffset myzoneoffset = systemDefaultZone.getRules().getOffset(instantObject);
        System.out.println(myzoneoffset);
    }
}

How to Convert ZoneOffset to ZoneId in java8?

java.time.ZoneId class has ofOffset method which creates ZoneId from zoneOffset and prefix

Here is a Syntax

public static ZoneId ofOffset(String prefix,   ZoneOffset offset)

The following code is the usage of ofOffset method

import java.time.ZoneId;
import java.time.ZoneOffset;
public class Test {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.ofOffset("GMT",ZoneOffset.UTC);
        System.out.println(zoneId); //GMT
    }
}

How to get offset value from UTC timezone?

Following are steps to get offset value.

  • First, you need to create an Instance of LocalDateTime.
  • LocalDateTime will give only the 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(Instant.now())
  • This gives the number of hours/minutes offset from UTC timezone difference
import java.time.Instant;
import java.time.ZoneId;
public class Test {
    public static void main(String[] args) {
        System.out.println(ZoneId.systemDefault().getRules().getOffset(Instant.now())); // +05:30

    }
}

ZoneOffset Methods

MethodDescription
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

Conclusion

Learn ZoneOffset class tutorials with below examples

  • How to Convert ZoneOffset to ZoneId in java8?
  • How to get offset value from UTC timezone?
  • How to Convert ZoneId to ZoneOffset in java8?
  • methods