Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM and timezones

Tags:

java

timezone

I'm having an issue with java timezones, if anyone can help me.

I have a web application running on tomcat 5.5 (not sure if this is relevant), with the following JVM version

[someuser@webserver bin]$ java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Server VM (build 1.5.0_06-b05, mixed mode)
[someuser@webserver bin]$

The system date is, say -

[someuser@webserver bin]$ date
Mon Aug 15 09:09:46 EST 2011

Within the web application, I make a call at a certain point to Calendar.getInstance().getTime(), and I print this timestamp in the logs.

Problem is that this timestamp gets returned in EDT, although server's time is in EST. For this reason, the date returned is 1h later than it should.

What I want to achieve is make Calendar.getInstance().getTime() to return a date in the same timezone as the system.

I have searched the forums, and found some suggestions that the jvm is not reading correctly the system's timezone. I have tried starting the tomcat with -Duser.timezone=EST parameter, but the system keeps returning timestamps in the EDT timezone. Please note - trying -Duser.timezone with a non-est parameter seems to work. The problems seems to be of a different nature.

My issue is somehow similar with this SO question. However, I'm only trying to get the date in the same timezone as the system is in, without any special handling.

Are you able to help?

like image 775
Andrei Avatar asked Aug 15 '11 14:08

Andrei


People also ask

How does Java handle time zones?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.

What is Java time zone?

TimeZone represents a time zone offset, and also figures out daylight savings. Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.

What is UTC TimeZone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.


1 Answers

I had the same issue. It turns out for me Java was looking in the /etc/sysconfig/clock file not the /etc/localtime file. This this for comment for more info

like image 114
mlathe Avatar answered Oct 25 '22 07:10

mlathe