Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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.

If you are using Maven:

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top