How to Set the JVM Timezone

Learn to set the default time zone used by the JVM using an environment variable, JVM argument and TimeZone class.

1. Overview

The users of any application want to see the dates and timestamps in their local timezone, and nobody likes to make timezone adjustments in their mind.

To show local date timestamps to users, the timezone used by the JVM must be predictable and preferably fixed. It makes the application’s unit testing and integration testing, concerning timezone-specific timestamps, easy and more reliable.

Setting the JVM time zone is more necessary in the distributed deployment models where the application is running across multiple data centers around the globe, and JVMs in each data center can be a different time zone.

  • By default, the JVM reads time zone information from the operating system and stores it in the TimeZone class.
  • To get the default time zone set in the JVM using the method TimeZone.getDefault().
  • To get the list of all supported timezones, use the method TimeZone.getAvailableIDs().
  • Java uses the naming convention of the tz database.

2. How to Set the Timezone for JVM

2.1. Set Environment Variable ‘TZ’

Set the environment variable TZ that JVM can use to get the default time zone in the JVM.

In Linux, we can use export command.

export TZ="Asia/Kolkata"

In windows, we can set the time zone as discussed using the Control Panel -> Date and Time -> Change Time Zone -> Select your preferred time zone option.

After setting the environment variable, we can verify it in our Java program.

TimeZone timezone = TimeZone.getDefault();
System.out.printf("DisplayName = %s, ID = %s, offset = %s",
        timeZone.getDisplayName(),timeZone.getID(),timeZone.getRawOffset());

Program Output.

DisplayName = Coordinated Universal Time, ID = UTC, offset = 0

2.2. Set JVM Argument or System Property ‘user.timezone’

If setting the environment variable is not possible then we can set the time zone using the JVM argument -Duser. timezone. Do not enclose the timezone value in double-quotes.

java -Duser.timezone=UTC com.app.Main 
//or
java -Duser.timezone=Asia/Kolkata com.app.Main 

The same argument, we can set using the system property "user.timezone".

System.setProperty("user.timezone", "UTC");

Now we can verify the updated timezone value in the Java programs.

TimeZone timezone = TimeZone.getDefault();
System.out.printf("DisplayName = %s, ID = %s, offset = %s",
        timeZone.getDisplayName(),timeZone.getID(),timeZone.getRawOffset());

Program Output.

DisplayName = Coordinated Universal Time, ID = UTC, offset = 0

2.3. TimeZone.setDefault()

Another way similar to setting system property is by setting the default time zone directly in the TimeZone class.

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

Verifying the timezone value will give the same result as in the previous techniques.

3. How JVM Resolves the Timezone

By default, Java Date and Time classes pick the time zone information from the operating system. The way, JVM resolves OS timezone, is different in each operating system.

One way to get the machine time zone is from the system clock and modify the desired timezone in the clock. But this approach is not possible in the cloud environment where resources are made available on demand.

We can alter the value of the timezone with the runtime JVM arguments and Java statements in the application.

Note that:

  • The TZ environment variable, if available, overrides the system’s default timezone.
  • JVM argument -Duser.timezone overrides TZ environment variable.
  • TimeZone.setDefault() overrides the -Duser.timezone argument.

4. Best Practices

  • Never rely on the default zone of the machine. Always specify your desired/expected time zone using one of the above-mentioned techniques.
  • An application can have time zone sensitive timestamps where after deploying the application in a cloud environment, the application could be moved to different data centers without our knowledge. To avoid these inconsistencies, it is recommended to set the JVM time zone using the -Duser.timezone system property.
  • If our requirement is to use a timezone not only by the JVM but also for all of its child processes, such as IDEs, then setting the TZ environment variable makes more sense. A good example of it is to set this variable when starting Eclipse and then you have it in all JVMs started by Eclipse.
  • We should prefer to use the longer names of the timezones instead of the three-letter IDs. There are short IDS that are duplicated among multiple time zones. For example, IST could be either India Standard Time, Irish Standard Time or Israel Standard Time.

5. Conclusion

In this tutorial, we learned to set the default time zone used by JVM in Unix and Windows operating systems.

As a best practice, always set the default timezone either at the JRE level or the application level to get consistent and expected timestamps.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode