Java Utility Library

Java TimeZone - getDefault() Method



The java.util.TimeZone.getDefault() method is used to get the default TimeZone of the Java virtual machine. If the cached default TimeZone is available, its clone is returned. Otherwise, the method takes the following steps to determine the default time zone.

  • Use the user.timezone property value as the default time zone ID if it's available.
  • Detect the platform time zone ID. The source of the platform time zone and ID mapping may vary with implementation.
  • Use GMT as the last resort if the given or detected time zone ID is unknown.

The default TimeZone created from the ID is cached, and its clone is returned. The user.timezone property value is set to the ID upon return.

Syntax

public static TimeZone getDefault()

Parameters

No parameter is required.

Return Value

Returns the default TimeZone.

Exception

NA

Example:

In the example below, the java.util.TimeZone.getDefault() method is used to get the default TimeZone of the Java virtual machine.

import java.util.*;

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

    //printing the default time zone value
    System.out.println("Default TimeZone is:\n" + tz);
  }
}

The possible output of the above code could be:

Default TimeZone is:
sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

❮ Java.util - TimeZone