java8 - java.time.ZoneId class tutorials with examples

What is ZoneId in Java?

ZoneId is an abstract class defined in java.time package. This is used to represent timezone such as Asia/Calcutta.

This provides rules for creating LocalDateTime and Instant objects.

Time in the earth or world is divided into regions. Whereas each part of the region is called a time zone. Timezone is an offset or number of hours difference from Standard time zone UTC - Coordinate Universal Time.

ZoneId class Syntax:

public abstract class ZoneId implements Serializable

How to create ZoneId Object?

It Provides the of() method for creating the specific timezone. This method takes a String of zone id that gets 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
        ZoneId zoneId4 = ZoneId.of("A");



Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: A
 at java.base/java.time.ZoneOffset.of(
 at java.base/java.time.ZoneId.of(
 at java.base/java.time.ZoneId.of(
 at org.cloudhadoop.LocalDateTime.ConvertToDate.main(

of() method takes the following things,
If the letter is Z, it takes Zone id as UTC, if you pass any letter other than Z, it throws ”Exception in thread “main” java.time.DateTimeException: Invalid ID for ZoneOffset“.
If it contains + or -, It considered as ZoneOffset as a value.
If it contains a string - Asia/Calcutta, that string is considered as zone id

Java ZoneId methods and examples

There are two methods in ZoneId class.

  • systemDefault() method: return current system default timezone - Asia/Calcutta
  • getId() method return the id of Zone id.
  • getDisplayName() method: return 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));



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


adjustInto(Temporal temporal)adjust the Offset of the caller with the same offset of the parameter object
from(TemporalAccessor temporal)Return the offset instance from temporal instance
getId()Return the offset id ie Z
get(TemporalField field)Return the field from an offset as Int value
getRules()Return timezone rules
systemDefault()Return Default timezone id
getLong()Return the field from an offset as a Long value
of(String offsetId)It is used to create a ZoneOffset object with id
ofHours(int hours)It is used to create a ZoneOffset object with offset hours
ofHoursMinutes(int offset Hours, int offsetMinutes)It is used to create a ZoneOffset object with offset hours and minutes
ofHoursMinutesSeconds(int hours, int minutes, int seconds)It is used to create a ZoneOffset object with offset hours, minutes, and seconds
ofTotalSeconds(int totalSeconds)It is used to create a ZoneOffset object with total seconds
range(TemporalField field)It is used to the range of values using Temporal field

How to get available all time zones in java?

ZoneId class has static getAvailableZoneIds() method - which return 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();;

The output of the above code is



Finally, In this tutorial, You learned java.time.ZoneId class in java8 and multiple examples using this class.