Posted by: lrrp | April 3, 2006

Making Java ignore daylight savings by Alan Williamson

Daylight savings hits most of us twice a year. Our desktop machines happily adjust to the correct time, moving forward or backward accordingly. But what of our servers?

Wrestling with Daylight savingsIn most server applications, it is often desirable to keep a constant time throughout the year. This is known as Universal Time Clock or UTC. This is particularly important if you have users from all over the world accessing the same machine and you want to ensure everyone is saving and manipulating a constant time value.

There are two things you need to do if you want your server to effectively ignore daylight savings. First thing is to make sure your server’s time is showing the right timezone. In Linux you can do this by setting your timezone to UTC:

% ln -sf /usr/share/zoneinfo/UTC /etc/localtime
% date
Sun Mar 26 11:43:17 UTC 2006

Depending on your Java configuration and installation, it my be reporting the wrong time as it uses its own rules for determining the time zone based on a variety of parameters passed including what the system time is telling it. There is a good article over at JavaWorld explaining this in more detail.

You can naturally set the Local and timezone within your Java code, but for enterprise shops this isn’t desirable. Instead you can pass in the timezone Java will use through the System Property user.timezone.

% java -Duser.timezone=UTC myJavaClass

This forces the JVM to use the UTC timezone, which means when you do any time related calls (java.util.Date etc) it will report the same time as the server is reporting.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: