Java Reference
In-Depth Information
Listing 12-3. Creating Instances of the ZoneOffset Class
// ZoneOffsetTest.java
package com.jdojo.datetime;
import java.time.ZoneOffset;
public class ZoneOffsetTest {
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);
}
}
-06:00
+05:30
+08:30:45
+05:00
Z
ZoneOffset.UTC: Z
ZoneOffset.MIN: -18:00
ZoneOffset.MAX: +18:00
According to ISO-8601 standards, a zone offset may include hours and minutes, or hours only. The new
Date-Time API also allows seconds in a zone offset. You can use the compareTo() method of the ZoneOffset class to
compare a zone offset to another zone offset. Zone offsets are compared in descending order that is the order in which
they occur in the time of the day, for example, a zone offset of +5:30 occurs before a zone offset of +5:00. ISO-8601
standards support zone offsets between -12:00 to +14:00. However, to avoid any problems in future if the zone offset
gets extended, the Date-Time API supports zone offsets between -18:00 to +18:00.
Search WWH ::




Custom Search