Core Java

Java 8 Display all ZoneId and its UTC offset Example

In this article we will see how to display all ZoneId and its UTC offset. A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime.

1. Introduction

The java.time package contains the main APIs for dates, times, instants and duration. The classes defined here represent the principle date-time concepts, including instants, durations, dates, times, time-zones and periods. They are based on the ISO calendar system, which is the de facto world calendar following the proleptic Gregorian rules. All the classes are immutable and thread-safe.

Each date time instance is composed of fields that are conveniently made available by the APIs. For lower level access to the fields refer to the java.time.temporal package. Each class includes support for printing and parsing all manner of dates and times. Refer to the java.time.format package for customisation options.

2. Java 8 Display ZoneId and its UTC offset – Example

In this section we will see a working example to display all Zone ids and their UTC offset. We will make use of the java.time.ZoneId class. This class was introduced in java 8. It is used to identify the rules used to convert between an Instant and a LocalDateTime. There are two distinct types of ID:

  • Fixed offsets – a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times
  • Geographical regions – an area where a specific set of rules for finding the offset from UTC/Greenwich apply

 
Most fixed offsets are represented by ZoneOffset. Calling normalized() on any ZoneId will ensure that a fixed offset ID will be represented as a ZoneOffset. The actual rules, describing when and how the offset changes, are defined by ZoneRules. This class is simply an ID used to obtain the underlying rules. This approach is taken because rules are defined by governments and change frequently, whereas the ID is stable.

The distinction has other effects. Serializing the ZoneId will only send the ID, whereas serializing the rules sends the entire data set. Similarly, a comparison of two IDs only examines the ID, whereas a comparison of two rules examines the entire data set.

Time-zone IDs

The ID is unique within the system. There are three types of ID. The simplest type of ID is that from ZoneOffset. This consists of ‘Z’ and IDs starting with ‘+’ or ‘-‘. The next type of ID are offset-style IDs with some form of prefix, such as ‘GMT+2’ or ‘UTC+01:00’. The recognised prefixes are ‘UTC’, ‘GMT’ and ‘UT’. The offset is the suffix and will be normalized during creation. These IDs can be normalized to a ZoneOffset using normalized(). The third type of ID are region-based IDs. A region-based ID must be of two or more characters, and not start with ‘UTC’, ‘GMT’, ‘UT’ ‘+’ or ‘-‘. Region-based IDs are defined by configuration, see ZoneRulesProvider. The configuration focuses on providing the lookup from the ID to the underlying ZoneRules.

Time-zone rules are defined by governments and change frequently. There are a number of organizations, known here as groups, that monitor time-zone changes and collate them. The default group is the IANA Time Zone Database (TZDB). Other organizations include IATA (the airline industry body) and Microsoft.

Each group defines its own format for the region ID it provides. The TZDB group defines IDs such as ‘Europe/London’ or ‘America/New_York’. TZDB IDs take precedence over other groups.

To get all the available zone ids call the getAvailableZoneIds() method of ZoneId. This set includes the string form of all available region-based IDs. Offset-based zone IDs are not included in the returned set. The ID can be passed to of(String) to create a ZoneId. The set of zone IDs can increase over time, although in a typical application the set of IDs is fixed. Each call to this method is thread-safe.

Set availableZoneIds = ZoneId.getAvailableZoneIds();

Now to get the offset use:

ZoneOffset offset = zoneId.getRules().getOffset(LocalDateTime.now());

Below is the full source-code of the file:

DisplayZoneId.java

package com.javacodegeeks;

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

public class DisplayZoneId {

    public static void main(String[] args) {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        for (String zone : availableZoneIds) {

            ZoneId zoneId = ZoneId.of(zone);
            ZoneOffset offset = zoneId.getRules().getOffset(LocalDateTime.now());
            System.out.println("Zone: " + zone + ", Offset: " + offset.toString());

        }
    }

}

When you will run the above class you will see output similar to below:

Zone: Asia/Aden, Offset: +03:00
Zone: America/Cuiaba, Offset: -04:00
Zone: Etc/GMT+9, Offset: -09:00
Zone: Etc/GMT+8, Offset: -08:00
Zone: Africa/Nairobi, Offset: +03:00
Zone: America/Marigot, Offset: -04:00
Zone: Asia/Aqtau, Offset: +05:00
Zone: Pacific/Kwajalein, Offset: +12:00
Zone: America/El_Salvador, Offset: -06:00
Zone: Asia/Pontianak, Offset: +07:00
...

3. Conclusion

In this article we learnt how to get all the available zone ids and how to get the UTC offset for those. A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime.

4. Download the Source Code

That was an example of Java 8 Display all ZoneId and its UTC offset.

Download
You can download the full source code of this example here: Display ZoneId

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button