Software Development

Override a Java application's time zone

There may be times when you need to override the time zone your Java application thinks it's in. Learn how to change the time zone for a particular application without changing the machine's time zone.

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[]) {
        System.out.println(new java.util.Date());
    }
}

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 is:

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!

2 comments
unamepwd786
unamepwd786

Actually my java code like this..... SimpleDateFormat sourceFormat = new SimpleDateFormat("z"); SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('Z')'"); date1 = gmtFormat.format(sourceFormat.parse("IST")); O/p:GMT(+0530) for example we are passing "PST" it will show GMT(-0800) but it will show always GMT(+0530)....

HAL 9000
HAL 9000

Try reposting this in the 'Q&A' forum. The 'Discussion' forum is for matters of general discussion, not specific problems in search of a solution. The 'Water Cooler' is for non-technical discussions. You can submit a question to 'Q&A' here: http://www.techrepublic.com/forum/questions/post?tag=mantle_skin;content There are TR members who specifically seek out problems in need of a solution. Although there is some overlap between the forums, you'll find more of those members in 'Q&A' than in 'Discussions' or 'Water Cooler'. Be sure to use the voting buttons to provide your feedback. Voting a '+' does not necessarily mean that a given response contained the complete solution to your problem, but that it served to guide you toward it. This is intended to serve as an aid to those who may in the future have a problem similar to yours. If they have a ready source of reference available, perhaps won't need to repeat questions previously asked and answered. If a post did contain the solution to your problem, you can also close the question by marking the helpful post as "The Answer". .

Editor's Picks