Java - Date Time ZoneId Class

Introduction

ZoneId class represents a combination of a zone offset and the rules for changing the zone offset for observed Daylight Saving Time.

Not all time zones observe Daylight Saving Time.

You can think of it as

ZoneId = ZoneOffset + ZoneRules

ZoneOffset represents a fixed zone offset from UtC time zone whereas a ZoneId represents a variable zone offset.

ZoneOffset class inherits from the ZoneId class.

Time zone format

A time zone has a unique textual ID, which can be specified in three formats:

  • zone ID is specified in terms of zone offset, which can be "Z", "+hh:mm:ss", or "-hh:mm:ss", for example, "+06:00".
  • zone ID is prefixed with "UTC", "GMT", or "UT" and followed by a zone offset, for example, "UTC+06:00".
  • zone ID is specified by using a region, for example, "America/Chicago".

The first two forms of Zone IDs create a ZoneId with a fixed zone offset. You can create a ZoneId using the of() factory method.

ZoneId usChicago = ZoneId.of("America/Chicago");
ZoneId bdDhaka = ZoneId.of("Asia/Dhaka");
ZoneId fixedZoneId = ZoneId.of("+06:00");

ZoneId class provides access to all known time zone IDs.

Its getAvailableZoneIds() static method returns a Set<String> containing all available zone Ids.

The following code shows how to print all zone IDs.

Demo

import java.time.ZoneId;
import java.util.Set;

public class Main {
        public static void main(String[] args) {
                Set<String> zoneIds = ZoneId.getAvailableZoneIds();
                for (String zoneId: zoneIds) {
                        System.out.println(zoneId);
                }//  w  w w .j  a  va2  s . c  o m
        }
}

Result

Related Topics