java8 - java.time.ZoneId class tutorials with examples

What is ZoneId in Java?

ZoneId is an abstract class defined in the java.time package. It is utilized to represent timezones, such as Asia/Calcutta.

This class provides rules for creating LocalDateTime and Instant objects.

Time on Earth is divided into regions, with each region being called a timezone.

A timezone represents an offset or the number of hours difference from the Standard timezone, UTC - Coordinated Universal Time.

ZoneId class Syntax:

public abstract class ZoneId implements Serializable

How to create a ZoneId Object?

It provides the of() method for creating a specific timezone. This method takes a String of the zone id obtained from the ZoneOffset.

import java.time.ZoneId;

public class ZoneIdExample {
    public static void main(String[] args) {
        ZoneId zoneId1 = ZoneId.of("Asia/Calcutta");
        ZoneId zoneId2 = ZoneId.of("Z"); // Z is an Zone id for UTC
        ZoneId zoneId3 = ZoneId.of("-8"); // -08:00
        System.out.println(zoneId1);
        System.out.println(zoneId2);
        System.out.println(zoneId3);
        ZoneId zoneId4 = ZoneId.of("A"); // throws Exception
        System.out.println(zoneId4);

    }
}

output:

Asia/Calcutta
Z
-08:00
Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: A
 at java.base/java.time.ZoneOffset.of(ZoneOffset.java:241)
 at java.base/java.time.ZoneId.of(ZoneId.java:402)
 at java.base/java.time.ZoneId.of(ZoneId.java:356)
 at org.cloudhadoop.LocalDateTime.ConvertToDate.main(ConvertToDate.java:17)

The of() method accepts the following

  • If the input is Z, it represents the UTC timezone. Any other letter throws an exception.”Exception in thread “main” java.time.DateTimeException: Invalid ID for ZoneOffset”.
  • If it contains + or -, it’s considered as a ZoneOffset value.
  • If it contains a string like Asia/Calcutta, that string is considered as a zone id.

Java ZoneId methods and examples

There are two methods in the ZoneId class

  • systemDefault() method: returns the current system default timezone, which is Asia/Calcutta.
  • getId() method: returns the id of the Zone id.
  • getDisplayName() method: returns the display name of the specific timezone using internalization.
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class ZoneIdSystemDefaultExample {
    public static void main(String[] args) {
        ZoneId defaultZone = ZoneId.systemDefault();
        System.out.println("Default System: " + defaultZone);
        System.out.println("Default System getId : " + defaultZone.getId());
        System.out.println("Default System Display Name : " + defaultZone.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH));
    }
}

Output:

Default System: Asia/Calcutta
Default System getId : Asia/Calcutta
Default System Display Name : India Time

Methods

MethodDescription
adjustInto(Temporal temporal)Adjusts the offset of the caller with the same offset of the parameter object
from(TemporalAccessor temporal)Returns the offset instance from the temporal instance
getId()Returns the offset id, i.e., Z
get(TemporalField field)Returns the field from an offset as an Int value
getRules()Returns timezone rules
systemDefault()Returns the Default timezone id
getLong()Returns the field from an offset as a Long value
of(String offsetId)Creates a ZoneOffset object with the given id
ofHours(int hours)Creates a ZoneOffset object with the given offset hours
ofHoursMinutes(int offset Hours, int offsetMinutes)Creates a ZoneOffset object with the given offset hours and minutes
ofHoursMinutesSeconds(int hours, int minutes, int seconds)Creates a ZoneOffset object with the given offset hours, minutes, and seconds
ofTotalSeconds(int totalSeconds)Creates a ZoneOffset object with the given total seconds
range(TemporalField field)Returns the range of values using the Temporal field

How to get all available time zones in Java?

The ZoneId class has a static getAvailableZoneIds() method which returns available timezone id strings.

import java.time.ZoneId;
import java.util.Set;
public class ConvertToDate {
 public static void main(String[] args) {
     Set availableZones = ZoneId.getAvailableZoneIds();
     availableZones.stream().forEach(System.out::println);
 }
}

The output of the above code is

Asia/Aden
America/Cuiaba
Etc/GMT+9
Etc/GMT+8
Africa/Nairobi
America/Marigot
Asia/Aqtau

Conclusion

In this tutorial, you learned about the java.time.ZoneId class in Java 8 and multiple examples using this class.