Documentation Contents
Java Platform, Standard Edition Troubleshooting Guide
Contents Previous Next

# 8.2 Determine the Default Time Zone on Windows

This section clarifies how the JRE determines the default time zone on the Windows Vista and Windows 7 operating systems. If Java doesn't report the expected time zone, use the troubleshooting techniques provided in the following sections:

- Check the Default Time Zone JRE Reports

- Determine the Setting in the Control Panel

- Check for Automatic Daylight Saving Time Adjustment

- Set the Default Time Zone in the Control Panel

- Check -Duser.timezone System Property

- Special Tools in Windows 7

- JRE Internal Representation of Time Zone Mappings

## 8.2.1 Check the Default Time Zone JRE Reports

You can write a simple program to determine which time zone the JRE reports as the default time zone, based on a check with the native operating system.

The Java program in Example 8-6 returns the default time zone:

Example 8-6 Java Code Snippet to Check the Time Zone Setting in the JRE

    public class DefaultTimeZone {
        public static void main(String[] args) {
            System.out.println(java.util.TimeZone.getDefault().getID());
        }
    }

You can save the code snippet in Example 8-6 to a file named `DefaultTimeZone.java` and compile it using the `javac` command. Then you can run the compiled `DefaultTimeZone` class, as shown in Example 8-7.

Example 8-7 Compiling and Running the DefaultTimeZone Program

    c:\tztest> javac DefaultTimeZone.java
    c:\tztest> java DefaultTimeZone
    Europe/Berlin

In Example 8-7, the default time zone is Europe/Berlin. Running the program should display your local time zone. If the output is not the expected time zone, then continue with the following troubleshooting steps.

## 8.2.2 Determine the Setting in the Control Panel

You can change or examine the system's default time zone using the Windows Control Panel. For example, you can select this time zone setting in Windows 7:

(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna

The corresponding value for the Registry key `TimeZoneKeyName` is W. Europe Standard Time.

## 8.2.3 Check for Automatic Daylight Saving Time Adjustment

You can check whether automatic adjustment of daylight saving time is enabled through the graphical user interface (GUI) or through the Windows registry.

- GUI Method: To use the Control Panel to check whether automatic adjustment of daylight saving time is enabled, follow these steps.

  1. Click the Windows Start button and then click Control Panel.

  2. Click Date and Time.

  3. Click the Change Time Zone button.

  4. There is a click box labeled Automatically adjust time for Daylight Savings Time. Observe whether or not this click box is clicked, and change the setting if you desire.

  5. Click OK. This returns you to the Date and Time dialog box.

- Windows Registry Method: You can run Windows Registry Editor to check whether automatic adjustment of daylight saving time is enabled.

  Caution: It is a good practice to back up the Windows registry before examining or editing it. If you make a mistake in editing, you can damage the Windows registry.

  To enable automatic adjustment of daylight saving time from the Windows registry:

  1. Click the Windows Start button.

  2. In the Search programs and files field, type regedit and then press Enter to open the Registry Editor.

  3. In the Registry Editor, search for the key DynamicDaylightTimeDisabled and examine the setting.

     If the registry setting is 1, then dynamic daylight time is disabled.

     If the registry setting is 0, then dynamic daylight time is enabled.

  If you prefer, you can access the Windows registry from the Windows command window.

In Example 8-8, the registry setting is 1. With this setting, the clock is not automatically adjusted for daylight saving time.

Example 8-8 DynamicDaylightTimeDisabled Registry Key Set to True

    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
    "DynamicDaylightTimeDisabled"=dword:00000001

If you have disabled the `DynamicDaylightTimeDisabled` option, then Java always returns a GMT (Greenwich mean time) offset and not a time zone ID that is compatible with the uniform naming convention (such as Europe/Berlin). For example, the offset will be expressed as GMT+01 and not Europe/Berlin.

## 8.2.4 Set the Default Time Zone in the Control Panel

You can change or examine the system's default time zone by using the Windows Control Panel.

To set the system's default time zone from the Control Panel:

1. Click the Windows Start button and then click Control Panel.

2. Click Date and Time.

3. Click the Change Time Zone button.

4. From the Time Zone menu, select your preferred time zone.

5. Click OK. This returns you to the Date and Time dialog box.

6. Click OK to close the Date and Time dialog box.

For example, you can select this time zone in Windows 7:

    (UTC)+1:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna.

The corresponding value for the Registry key `TimeZoneKeyName` is W. Europe Standard Time.

## 8.2.5 Check -Duser.timezone System Property

You can explicitly set a default time zone on the command line by using the Java system property called `user.timezone`. This bypasses the settings in the Windows operating system and can be a workaround in some cases. For instance, this setting is useful if you want DST (daylight saving time) only for a single Java program running on the system.

The following example examines the system property -Duser.timezone by running a Java program called DefaultTimeTestZone from the Windows Command Prompt window.

`c:\tztest> java -Duser.timezone=America/New_York DefaultTimeZone
America/New_York`

If setting a default time zone explicitly by specifying `-Duser.timezone` works for the `DefaultTimeTestZone` program, but does not work for your program, you should check whether your code overwrites the default Java time zone during runtime, with a method call such as this:

    TimeZone.setDefault(TimeZone zone)

## 8.2.6 Special Tools in Windows 7

With Windows 7, a tool called `tzutil.exe` is available. With this tool, you can request the current time zone ID abbreviation without manually reading the registry.

Here is an example of running `tzutil.exe`. The first line is the command that you enter in Windows Command Prompt window. The second line is the system response.

    tzutil /g

    W. Europe Standard Time

## 8.2.7 JRE Internal Representation of Time Zone Mappings

On Windows, the JRE uses a file <java-home>`\lib\tzmappings` to represent the mapping between Windows and Java time zones. Each line in the file has four tokens. The first token is the Windows time zone registry key called `TimeZoneKeyName`. See Determine the Setting in the Control Panel.

The second token is a time zone map ID. (This is not used in Windows Vista and Windows 7.) The third token is the locale. The fourth token represents the Java time zone ID. The important tokens are token number one, number three (which can be empty), and number four. (Note: This file is not a public interface.)

If you select the time zone called (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna in the Windows Control Panel, then the relevant line in the file `tzmappings` is:

`W. Europe Standard Time:2,3::Europe/Berlin:`

In this example, the JRE recognizes your default time zone (token number four) as Europe/Berlin.

If there is no appropriate mapping entry in the tzmappings file, then it is possible that Microsoft introduced a new time zone in a Windows update, and that the new time zone is not available to the JRE. In this situation, you can file a bug report for the JRE and request a new entry in the tzmappings file from Oracle Java bugs website.

A similar disconnect between the operating system and the JRE is possible if you have run the tool `tzedit.exe.` This tool is posted by Microsoft on the Internet, and allows users to add new time zones. The JRE is unlikely to have a time zone introduced into the system by this tool. Again, the solution is to file a bug to request that a new entry be added to the `tzmappings` file.

Contents Previous Next

Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved.
Contact Us