Get the ID of this timezone in Java



To get the ID of the current timezone in Java, we use the ZoneId class that belongs to java.time package. This provides a way to work with different timezones and their IDs.

In Java, the ZoneId class represents a time zone identifier, such as India/Europe. The ZoneId can be used to retrieve the unique ID of the current timezone.

To Get the ID of this timezone in Java is quite easy. Let's learn the following methods:

Using ZoneId Class

To get the ID, we have a class named ZoneId that belongs to java.time package that represents a time zone identifier, such as Europe/Paris. It is used to work and manipulate time zone data.

Example

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class GetCurrentTimeZoneID {
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        ZoneId zoneId = now.getZone();
        System.out.println("The current timezone ID is: " + zoneId);
    }
}

Output

The above program produce the following result ?

The current timezone ID is: Asia/Kolkata

Using ZoneId.systemDefault() Method

To get the ID, we also have a method named ZoneId.systemDefault() which is a static method of the ZoneId class in the java.time package. It returns the system's default time zone.

Example

import java.time.ZoneId;

public class GetCurrentTimeZoneID {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("The current timezone ID is: " + zoneId);
    }
}

Output

The above program produce the following result ?

The current timezone ID is: Asia/Kolkata

Using TimeZone.getDefault().toZoneId() Method

To get the ID, we have another method named TimeZone.getDefault().toZoneId() method in Java is used to obtain the system's default time zone and convert it to a ZoneId object.

Example

import java.util.TimeZone;
import java.time.ZoneId;

public class GetCurrentTimeZoneID {
    public static void main(String[] args) {
        ZoneId zoneId = TimeZone.getDefault().toZoneId();
        System.out.println("The current timezone ID is: " + zoneId);
    }
}

Output

The above program produce the following result ?

The current timezone ID is: Asia/Kolkata
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-14T03:34:12+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements