Java Utility Library

Java TimeZone - toZoneId() Method



The java.util.TimeZone.toZoneId() method is used to convert this TimeZone object to a ZoneId.

Syntax

public ZoneId toZoneId()

Parameters

No parameter is required.

Return Value

Returns a ZoneId representing the same time zone as this TimeZone.

Exception

NA

Example:

In the example below, the java.util.TimeZone.toZoneId() method is used to convert the given TimeZone object to a ZoneId.

import java.util.*;
import java.time.*;  

public class MyClass {
  public static void main(String[] args) {
    //creating a TimeZone object
    TimeZone tz = TimeZone.getTimeZone("IST");

    //getting the zoneid from the timezone object
    ZoneId zid = tz.toZoneId();

    //printing the time zone value
    System.out.println("The ZoneId is: " + zid);
  }
}

The output of the above code will be:

The ZoneId is: Asia/Kolkata

❮ Java.util - TimeZone