ZoneId

Description

ZoneId represents a zone offset and its rules for changing the zone offset for Daylight Saving Time.

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

What are the changes is defined by time zone rules.

Time Zone ID

Each time zone has an ID which can be defined in three formats:

  • In zone offset, which can be "Z", "+hh:mm:ss", or "-hh:mm:ss", for example, "+01:00".
  • Prefixed with "UTC", "GMT", or "UT" and followed by a zone offset, for example, "UTC+01:00".
  • In region name, for example, "America/Chicago".

Example

The following code shows how to create a ZoneId using the of() factory method.


import java.time.ZoneId;
//  w  ww .  j a  va 2  s. c om
public class Main {
  public static void main(String[] args) {
    ZoneId  usChicago   = ZoneId.of("America/Chicago");
    System.out.println(usChicago);
    ZoneId  fixedZoneId = ZoneId.of("+01:00");
    System.out.println(fixedZoneId);
  }
}

The code above generates the following result.

Example 2

getAvailableZoneIds() from ZoneId returns all known time zone IDs.


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

The code above generates the following result.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial