ZoneId class

The ZoneId class represents a time zone identifier, used to manage and apply time zone rules. It provides access to the rules and names of various time zones (like Asia/Kolkata, America/New_York, etc.).

It is often used with ZonedDateTime to attach a time zone to a date-time.

Commonly Used Methods

Simple Program

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

public class ZoneIdExample {
    public static void main(String[] args) {
        ZoneId defaultZone = ZoneId.systemDefault();
        ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");

        System.out.println("System Default Zone: " + defaultZone);
        System.out.println("Tokyo Zone: " + tokyoZone);

        System.out.println("\nSample of Available Zone IDs:");
        Set<String> zones = ZoneId.getAvailableZoneIds();
        zones.stream().limit(5).forEach(System.out::println); // show only 5 for brevity
    }
}

Problem Statement

LotusJavaPrince and LotusPythonPrince are developing a Time Zone Picker Tool for travelers. When a user selects their location, the tool should:

  • Display the system’s current default zone.
  • List the top 5 time zones containing “Asia”.
  • Let the user select a zone and display the full zone ID and its rules.
import java.time.ZoneId;
import java.time.zone.ZoneRules;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

public class TimeZonePicker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to TimeZone Picker by LotusJavaPrince & Mahesh!");

        // Show system default zone
        ZoneId defaultZone = ZoneId.systemDefault();
        System.out.println("System Default Zone: " + defaultZone);

        // Filter and show top 5 Asian time zones
        Set<String> asianZones = ZoneId.getAvailableZoneIds().stream()
                .filter(id -> id.startsWith("Asia/"))
                .limit(5)
                .collect(Collectors.toSet());

        System.out.println("\nTop 5 Asian Time Zones:");
        asianZones.forEach(System.out::println);

        // Prompt user for a zone
        System.out.print("\nEnter a time zone from above (e.g., Asia/Kolkata): ");
        String inputZone = scanner.nextLine();

        try {
            ZoneId selectedZone = ZoneId.of(inputZone);
            ZoneRules rules = selectedZone.getRules();

            System.out.println("Selected Zone ID: " + selectedZone);
            System.out.println("Is Daylight Saving in effect? " + rules.isDaylightSavings(java.time.Instant.now()));
        } catch (Exception e) {
            System.out.println("Invalid time zone ID.");
        }

        scanner.close();
    }
}
/*
Welcome to TimeZone Picker by LotusJavaPrince & Mahesh!
System Default Zone: Asia/Kolkata

Top 5 Asian Time Zones:
Asia/Kolkata
Asia/Tokyo
Asia/Dubai
Asia/Shanghai
Asia/Seoul

Enter a time zone from above (e.g., Asia/Kolkata): Asia/Tokyo
Selected Zone ID: Asia/Tokyo
Is Daylight Saving in effect? false
*/

The ZoneId class is the foundation of time zone-aware programming in Java. It helps you:

  • Work with over 500+ official time zones.
  • Associate a ZoneId with date-time classes like ZonedDateTime.
  • Easily manage and convert across time zones using proper rules.

Use ZoneId whenever your application handles geographically aware time data, especially when working across regions.

Scroll to Top