Your application’s time zone is usually
determined by the machine’s time zone on which the app is running.
Sometimes, however, you may need to override the time zone the
application thinks it’s in.

To change the time zone for a particular
application without changing the machine’s time zone, set the
system property user.timezone to the time zone that you want your
program to run in. You can set this system property when you start
the virtual machine by passing a command line parameter:

java -Duser.timezone=UTC TimeZoneTip

The class below creates a Date object and
prints it to standard out:

public class TimeZoneTip {
    public static void main(String args[])

When the program is run without the
user.timezone property set, the output is:

Sun Jun 28 19:38:27 EDT 2003

In this case, the time zone defaulted to the
time zone of the machine the program was running on: Eastern
Daylight Time (EDT).

When the program is run with the user.timezone
property set to Coordinated Universal Time (UTC), the output

Sun Jun 28 23:39:59 UTC 2003

When you need an application to run in a
particular time zone, the solution is just a switch away. There are
other system properties that can affect your application’s
environment. Check out the tool documentation for the Java virtual
machine to find out more.

Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically sign up today!