145

I am trying to run a Java program, but it is taking a default GMT timezone instead of an OS defined timezone. My JDK version is 1.5 and the OS is Windows Server Enterprise (2007)

Windows has a Central timezone specified, but when I run the following program, it gives me a GMT time.

import java.util.Calendar;

public class DateTest
{
    public static void main(String[] args)
    {
        Calendar now = Calendar.getInstance();
        System.out.println(now.getTimeZone());
        System.out.println(now.getTime());
    }
}

Here is the output

sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null]
Mon Mar 22 13:46:45 GMT 2010

Please note that I do not want to set the timezone from the application. I want that the timezone used by JVM should be the one specified in the OS. (I am not finding this issues with other servers that have version 1.4 of JDK and Microsoft Server 2003).

Any thoughts would be highly appreciated.

Shashank Agrawal
  • 21,660
  • 9
  • 71
  • 105
Kushal Paudyal
  • 3,033
  • 4
  • 20
  • 30

8 Answers8

233

You can pass the JVM this param

-Duser.timezone

For example

-Duser.timezone=Europe/Sofia

and this should do the trick. Setting the environment variable TZ also does the trick on Linux.

Axel Fontaine
  • 31,019
  • 13
  • 93
  • 129
Bozhidar Batsov
  • 52,348
  • 13
  • 95
  • 111
  • We had to use the JDK DST Timezone Update Tool - 1.3.25 (tzupdater) and then change the JVM parameter as you suggested and we finally got rid of the issue. By the way, the timezone information is stored by Windows in some registry. The updater I used updated the registry values also. – Kushal Paudyal Mar 25 '10 at 21:17
  • 1
    I'm trying to startup my JVM using this parameter, but seems to have no change... If I want to start on UTC what shall I use as parameter!? – rafa.ferreira May 19 '11 at 21:43
  • In my application I am changing some locations and getting timezones of that locations.Using the timezone I am processing further.But when the application starts for the first time,I am getting correct values. When i change the TimeZone and set the new timezone as default timezone, I am not getting the correct values.What is the issue? How to change the timezone exactly.I am changing the timezone using TimeZone.setDefault(TimeZone.getTimeZone("my_time_zone")); – Dray Aug 20 '12 at 14:40
  • 3
    A list of the timezones for TZ is here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. Using the timezone name is better than the offset because it will handle daylight savings. – Brian May 15 '17 at 01:05
67

You can also set the default time zone in your code by using following code.

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

To Yours

 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Sofia"));
Ali786
  • 4,211
  • 6
  • 34
  • 54
24

The accepted answer above:

-Duser.timezone="Europe/Sofia" 

Didn't work for me exactly. I only was able to successfully change my timezone when I didn't have quotes around the parameters:

-Duser.timezone=Europe/Sofia
WN4yhihiY
  • 348
  • 2
  • 5
11

On windows 7 and for JDK6, I had to add -Duser.timezone="Europe/Sofia" to the JAVA_TOOL_OPTIONS system variable located under "My computer=>Properties=>Advanced System Settings=>Environment Variables".

If you already have some other property set for JAVA_TOOL_OPTIONS just append a space and then insert your property string.

Jens
  • 60,806
  • 15
  • 81
  • 95
VolkanT
  • 469
  • 5
  • 9
6

If you are using Maven:

mvn -Dexec.args="-Duser.timezone=Europe/Sofia ....."
MichalSv
  • 537
  • 5
  • 10
6

Two options that I don’t think were covered in the other answers:

Avoid the need

Whatever you do to set the JVM default time zone, it is very hard to make sure that no one else sets it differently. It can be set at any time without notice from another part of your program or from another program running in the same JVM. So in your time operations be explicit about which time zone you want, and you will always know what you get independently of the JVM setting. Example:

    System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));

Example output:

2018-10-11T14:59:16.742020+05:00[Asia/Dushanbe]

System.setProperty

For many purposes the following will not be the preferred way, and it can certainly be misused. For “throw away” programs I sometimes find it practical. You can also set a system property from within Java:

    System.setProperty("user.timezone", "Australia/Tasmania");
    System.out.println(ZonedDateTime.now());

This just printed:

2018-10-11T21:03:12.218959+11:00[Australia/Tasmania]

If you want validation of the string you are passing, use:

        System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());
Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
0

In win7, if you want to set the correct timezone as a parameter in JRE, you have to edit the file deployment.properties stored in path c:\users\%username%\appdata\locallow\sun\java\deployment adding the string deployment.javaws.jre.1.args=-Duser.timezone\=my_time_zone

Mani
  • 17,226
  • 13
  • 73
  • 97
Dave
  • 9
  • 1
0

Setting environment variable TZ should also works

ex: export TZ=Asia/Shanghai

Hunger
  • 4,328
  • 5
  • 18
  • 27