How to set a JVM TimeZone Properly

JavaJvmWindows Server-2008Timezonejdk1.5

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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"));

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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());

Solution 6 - Java

If you are using Maven:

mvn -Dexec.args="-Duser.timezone=Europe/Sofia ....."

Solution 7 - Java

Setting environment variable TZ should also works

ex: export TZ=Asia/Shanghai

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKushal PaudyalView Question on Stackoverflow
Solution 1 - JavaBozhidar BatsovView Answer on Stackoverflow
Solution 2 - JavasmaliView Answer on Stackoverflow
Solution 3 - JavaWN4yhihiYView Answer on Stackoverflow
Solution 4 - JavaVolkanTView Answer on Stackoverflow
Solution 5 - JavaOle V.V.View Answer on Stackoverflow
Solution 6 - JavaMichalSvView Answer on Stackoverflow
Solution 7 - JavaHungerView Answer on Stackoverflow
Solution 8 - JavaDaveView Answer on Stackoverflow