Java - Date Time ZoneOffset Class

Introduction

ZoneOffset class represents a fixed zone offset from UTC time zone, for example, +05:30, -06:00, etc.

It is a period of time that a time zone differs from the UTC.

A ZoneOffset is not aware of the changes in zone offset because of the observed Daylight Saving Time.

Constants

The ZoneOffset class declares three constants:

  • UTC
  • MAX
  • MIN

UTC is the time zone offset constant for UTC.

MAX and MIN are the maximum and minimum supported zone offsets.

Z, not +00:00 or -00:00, is used as the zone offset designator for UtC time zone.

ZoneOffset class provides methods to create its instances using a combination of hour, minute, and second.

The following code demonstrates how to create instances of the ZoneOffset class.

Demo

import java.time.ZoneOffset;

public class Main {
        public static void main(String[] args) {
                // Create zone offset using hour, minute, and second
                ZoneOffset zos1 = ZoneOffset.ofHours(-6);
                ZoneOffset zos2 = ZoneOffset.ofHoursMinutes(5, 30);
                ZoneOffset zos3 = ZoneOffset.ofHoursMinutesSeconds(8, 30, 45);
                System.out.println(zos1);
                System.out.println(zos2);
                System.out.println(zos3);

                // Create zone offset using offset ID as a string
                ZoneOffset zos4 = ZoneOffset.of("+05:00");
                ZoneOffset zos5 = ZoneOffset.of("Z"); // Same as ZoneOffset.UTC
                System.out.println(zos4);
                System.out.println(zos5);

                // Print the values for zone offset constants
                System.out.println("ZoneOffset.UTC: "  + ZoneOffset.UTC);
                System.out.println("ZoneOffset.MIN: "  + ZoneOffset.MIN);
                System.out.println("ZoneOffset.MAX: "  + ZoneOffset.MAX);
        }// ww  w  . j  a  v  a 2 s . co  m
}

Result