[java] How to set a JVM TimeZone Properly

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.

This question is related to java jvm windows-server-2008 timezone jdk1.5

The answer is


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


If you are using Maven:

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

Setting environment variable TZ should also works

ex: export TZ=Asia/Shanghai


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

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

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.


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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jvm

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How can I get a random number in Kotlin? Kotlin unresolved reference in IntelliJ Is JVM ARGS '-Xms1024m -Xmx2048m' still useful in Java 8? Android Gradle Could not reserve enough space for object heap Android java.exe finished with non-zero exit value 1 Android Studio Gradle project "Unable to start the daemon process /initialization of VM" Android Studio - No JVM Installation found Android Studio error: "Environment variable does not point to a valid JVM installation" Installing Android Studio, does not point to a valid JVM installation error

Examples related to windows-server-2008

Multiple -and -or in PowerShell Where-Object statement The FastCGI process exited unexpectedly Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory How can I enable the Windows Server Task Scheduler History recording? IIS7: A process serving application pool 'YYYYY' suffered a fatal communication error with the Windows Process Activation Service Gradle proxy configuration Installing Tomcat 7 as Service on Windows Server 2008 How to get current user who's accessing an ASP.NET application? ASP.Net which user account running Web Service on IIS 7? A process crashed in windows .. Crash dump location

Examples related to timezone

How to set the timezone in Django? How to convert Moment.js date to users local timezone? What does this format means T00:00:00.000Z? How do I get the current timezone name in Postgres 9.3? MySQL JDBC Driver 5.1.33 - Time Zone Issue Python get current time in right timezone Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings PHP date() with timezone? How to store a datetime in MySQL with timezone info Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST

Examples related to jdk1.5

How to set a JVM TimeZone Properly How to redirect verbose garbage collection output to a file? How do I join two lists in Java?