ZoneOffset

Description

ZoneOffset represents a fixed zone offset from UTC time zone.

ZoneOffset doesn't track the changes in zone offset caused by the Daylight Saving Time.

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 is used as the zone offset designator for UtC time zone.

Example

We can create ZoneOffset with a combination of hour, minute, and second.


import java.time.ZoneOffset;
//from  ww  w .  j a v  a  2s. co m
public class Main {
  public static void main(String[] args) {
    ZoneOffset zoneOffset1  = ZoneOffset.ofHours(-1);
    System.out.println(zoneOffset1);
    ZoneOffset zoneOffset2  = ZoneOffset.ofHoursMinutes(6, 30);
    System.out.println(zoneOffset2);
    ZoneOffset zoneOffset3  = ZoneOffset.ofHoursMinutesSeconds(9, 30,  45); 
    System.out.println(zoneOffset3);
  }
}

The code above generates the following result.

Example 2

The following code shows how to create zone offset from offset.


import java.time.ZoneOffset;
/*from  w  w  w . j  ava2  s.c o  m*/
public class Main {
  public static void main(String[] args) {
    ZoneOffset zoneOffset1  = ZoneOffset.of("+05:00");
    ZoneOffset zoneOffset2  = ZoneOffset.of("Z"); // Same as ZoneOffset.UTC 
    System.out.println(zoneOffset1);
    System.out.println(zoneOffset2);

  }
}

The code above generates the following result.

Example 3

The following code outputs the constant values from ZoneOffset.


import java.time.ZoneOffset;
// w  ww  . j  ava2  s  . com
public class Main {
  public static void main(String[] args) {
    System.out.println("ZoneOffset.UTC: "    + ZoneOffset.UTC); 
    System.out.println("ZoneOffset.MIN: "    + ZoneOffset.MIN); 
    System.out.println("ZoneOffset.MAX: "    + ZoneOffset.MAX);


  }
}

The code above generates the following result.

Note

Java Date-Time API supports zone offsets in hours and minutes, and seconds.

compareTo() from ZoneOffset allows us to compare two zone offsets.

A zone offset of +1:30 is before a zone offset of +1:00.

Java Date-Time API supports zone offsets between -18:00 to +18:00.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial