Core Java

Java 8 Display all ZoneId and its UTC offset Example

Photo of Mohammad Meraj ZiaMohammad Meraj ZiaOctober 3rd, 2018Last Updated: February 25th, 2019
0 344 3 minutes read

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 anInstant and aLocalDateTime.

1. Introduction

Thejava.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 thejava.time.temporal package. Each class includes support for printing and parsing all manner of dates and times. Refer to thejava.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 thejava.time.ZoneId class. This class was introduced in java 8. It is used to identify the rules used to convert between anInstant and aLocalDateTime. 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 byZoneOffset. Callingnormalized() on anyZoneId will ensure that a fixed offset ID will be represented as aZoneOffset. The actual rules, describing when and how the offset changes, are defined byZoneRules. 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 theZoneId 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 fromZoneOffset. 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 aZoneOffset usingnormalized(). 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, seeZoneRulesProvider. The configuration focuses on providing the lookup from the ID to the underlyingZoneRules.

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.

Want to be a Java 8 Ninja ?
Subscribe to our newsletter and download the Java 8 FeaturesUltimateGuideright now!
In order to get you up to speed with the major Java 8 release, we have compiled a kick-ass guide with all the new features and goodies! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

To get all the available zone ids call thegetAvailableZoneIds() method ofZoneId. 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 toof(String) to create aZoneId. 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:00Zone: America/Cuiaba, Offset: -04:00Zone: Etc/GMT+9, Offset: -09:00Zone: Etc/GMT+8, Offset: -08:00Zone: Africa/Nairobi, Offset: +03:00Zone: America/Marigot, Offset: -04:00Zone: Asia/Aqtau, Offset: +05:00Zone: Pacific/Kwajalein, Offset: +12:00Zone: America/El_Salvador, Offset: -06:00Zone: 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 anInstant and aLocalDateTime.

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
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Mohammad Meraj ZiaMohammad Meraj ZiaOctober 3rd, 2018Last Updated: February 25th, 2019
0 344 3 minutes read
Photo of Mohammad Meraj Zia

Mohammad Meraj Zia

Senior Java Developer

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.