Java timezone setting in Centos for Asia/Kuala_Lumpur

February 1, 2008 at 7:52 pm | Posted in App Servers, Java, Programming | 1 Comment

  

Is your application showing the wrong time? You’re sure you have set your operating system’s timezone and date time correctly?

Follow the step-by-step below to see if you have got your java application time settings correctly covered. The following has been tested on Centos5 with Java 6.0:

1)      Operating system timezone

During installation, you should’ve correctly set your operating system timezone via the GUI. Centos UI is quite similar with Redhat Linux, so here’s the interface http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-timezone-s390.html.

At terminal console, you can check if your date is correctly printed with command

For example, if my server is in Malaysia

>date

 

If the above does not apply, you can do it the command way. Copy the correct timezone file to /etc/localtime

Eg.

cp /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime

Verify it

>date

2)      Java application timezone

You can set the user.timezone system property in your java application.

During the startup invocation:

java -Duser.timezone=Asia/Kuala_Lumpur MyJavaClass 

Or, in the program source code:

System.setProperty(“user.timezone”,”Asia/Kuala_Lumpur”); 

Verify it

System.out.println(“The date is=” + new Date());  

3)      TZ environment timezone

If all fails, try setting the TZ environment variable

 export TZ=”Asia/Kuala_Lumpur” 

Verify that it has been set correctly

echo $TZ 

Then proceed to launch your java application, check if the output of new Date() is printing the correct timezone date and time.

  

You may want to make this setting persistent by including the

export TZ=”Asia/Kuala_Lumpur”

in your /etc/profile or /etc/init.d initialization script

Note:

Please ensure you know the consequences of setting or changing your timezone.

While finding the solution to my timezone problem, I have run all the steps above and it finally worked on Step 3. I have also tried an update on the TZDB but it does not help.

  

References

http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-timezone-s390.html

http://lists.centos.org/pipermail/centos/2005-May/047059.html

http://www.javaworld.com/javaworld/jw-12-2006/jw-1201-dst.html?page=2

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzamy/50/admin/reftzval.htm

http://minaret.biz/tips/timezone.html

Next Page »

Blog at WordPress.com.
Entries and comments feeds.