Java ZoneId

The Java ZoneId abstract class is used to distinguish the rules used to convert between an Instant and LocalDateTime.

How to use

The class ZoneId represents a time-zone ID, such as Asia/Calcutta or Asia/Tokyo. Try the following examples to better understand –

System default example

The following example program shows how you can use the ZoneId class to get the system default zone.

import java.time.ZoneId;

public class ZoneIdExample1 {

    public static void main(String[] args) {

        // Get system default zone id
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);
    }
}

Display name example

The following example program shows how you can use getDisplayName() function to get the display name of a zone.

import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class ZoneIdExample2 {

    public static void main(String[] args) {

        // Get system default zone id
        ZoneId zoneId = ZoneId.of("Asia/Calcutta");

        // Get display name
        String displayName = zoneId.getDisplayName(TextStyle.FULL, Locale.ENGLISH);

        System.out.println(displayName);
    }
}

Time of different zone example

The following example program shows how you can use ZoneId class with LocalTime class to get the time of different zones.

import java.time.LocalTime;
import java.time.ZoneId;

public class ZoneIdExample3 {

    public static void main(String[] args) {

        // Get system default zone id
        ZoneId zoneIdCalcutta = ZoneId.of("Asia/Calcutta");
        ZoneId zoneIdTokyo = ZoneId.of("Asia/Tokyo");

        LocalTime localTimeCalcutta = LocalTime.now(zoneIdCalcutta);
        LocalTime localTimeTokyo = LocalTime.now(zoneIdTokyo);

        // Print time at Asia/Calcutta;
        System.out.println(localTimeCalcutta);
        
        // Print time at Asia/Tokyo
        System.out.println(localTimeTokyo);
    }
}