Java ZoneOffset
The Java ZoneOffset class is an immutable and thread-safe class that represents the fixed zone offset from UTC time zone. Different parts of the world have several time-zone offsets.
How to use
The zone-offset is usually a fixed number of hours and minutes. The ZoneOffset class can be used to represent a time-zone offset from Greenwich/UTC, such as +02:00. Try the following examples to better understand –
ZoneOffset from UTC example
The following example program shows how you can create an instance of ZoneOffset with UTC and print the zone id.
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.Temporal; public class ZoneOffsetExample1 { public static void main(String[] args) { // Zone offset from UTC ZoneOffset zone = ZoneOffset.UTC; // Print id System.out.println(zone.getId()); Temporal temp = zone.adjustInto(ZonedDateTime.now()); System.out.println(temp); } }
ZoneOffset ofHoursMinutesSeconds() example
The following example program shows how you can ofHoursMinutesSeconds() function to create instance of a ZoneOffset.
import java.time.ZoneOffset; public class ZoneOffsetExample2 { public static void main(String[] args) { // Zone offset from hours minutes and seconds. ZoneOffset zone = ZoneOffset.ofHoursMinutesSeconds(02, 05, 30); // Print the id System.out.println(zone.getId()); } }
ZoneOffset isSupported() example
The following example program shows how you can use isSupported() function with ZoneOffset.
import java.time.ZoneOffset; import java.time.temporal.ChronoField; public class ZoneOffsetExample3 { public static void main(String[] args) { // Zone offset from UTC ZoneOffset zone = ZoneOffset.UTC; // Offset seconds supported? boolean supported = zone.isSupported(ChronoField.OFFSET_SECONDS); System.out.println(supported); // Hour of date supported? supported = zone.isSupported(ChronoField.HOUR_OF_DAY); System.out.println(supported); } }