Java Utility Library

Java TimeZone - setDefault() Method



The java.util.TimeZone.setDefault() method is used to set the TimeZone that is returned by the getDefault method.

Syntax

public static void setDefault(TimeZone zone)

Parameters

zone Specify the new default TimeZone, or null.

Return Value

void type.

Exception

Throws SecurityException, if the security manager's checkPermission denies PropertyPermission("user.timezone", "write")

Example:

In the example below, the java.util.TimeZone.setDefault() method is used to set the default TimeZone.

import java.util.*;

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

    //setting the default TimeZone to IST
    TimeZone.setDefault(tz);

    //get the display name of the
    //default TimeZone object
    Object tzdefault = TimeZone.getDefault().getDisplayName();

    //printing the display name
    System.out.println("Default TimeZone is: " + tzdefault);
  }
}

The output of the above code will be:

Default TimeZone is: India Standard Time

❮ Java.util - TimeZone