Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Time Zone is messed up

I am running a Tomcat application, and I need to display some time values. Unfortunately, the time is coming up an hour off. I looked into it and discovered that my default TimeZone is being set to:

sun.util.calendar.ZoneInfo[id="GMT-08:00",
                           offset=-28800000,
                           dstSavings=0,
                           useDaylight=false,
                           transitions=0,
                           lastRule=null]

Rather than the Pacific time zone. This is further indicated when I try to print the default time zone's display name, and it comes up "GMT-08:00", which seems to indicate to me that it is not correctly set to the US Pacific time zone. I am running on Ubuntu Hardy Heron, upgraded from Gutsy Gibbon.

Is there a configuration file I can update to tell the JRE to use Pacific with all the associated daylight savings time information? The time on my machine shows correctly, so it doesn't seem to be an OS-wide misconfiguration.


Ok, here's an update. A coworker suggested I update JAVA_OPTS in my /etc/profile to include "-Duser.timezone=US/Pacific", which worked (I also saw CATALINA_OPTS, which I updated as well). Actually, I just exported the change into the variables rather than use the new /etc/profile (a reboot later will pick up the changes and I will be golden).

However, I still think there is a better solution... there should be a configuration for Java somewhere that says what timezone it is using, or how it is grabbing the timezone. If someone knows such a setting, that would be awesome, but for now this is a decent workaround.


I am using 1.5, and it is most definitely a DST problem. As you can see, the time zone is set to not use daylight savings. My belief is it is generically set to -8 offset rather than the specific Pacific timezone. Since the generic -8 offset has no daylight savings info, it's of course not using it, but the question is, where do I tell Java to use Pacific time zone when it starts up? I'm NOT looking for a programmatic solution, it should be a configuration solution.

like image 476
Mike Stone Avatar asked Aug 08 '08 21:08

Mike Stone


People also ask

How does Java handle time zone issues?

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.

How do I change the timezone in Java?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

What is the default timezone in Java?

According to javadoc the java. util. Date class represents number of milliseconds since the standard base time known as "the epoch", namely 1 January 1970, 00:00:00 GMT.

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.


3 Answers

It's a "quirk" in the way the JVM looks up the zoneinfo file. See Bug ID 6456628.

The easiest workaround is to make /etc/localtime a symlink to the correct zoneinfo file. For Pacific time, the following commands should work:

# sudo cp /etc/localtime /etc/localtime.dist
# sudo ln -fs /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

I haven't had any problems with the symlink approach.

Edit: Added "sudo" to the commands.

like image 85
Jason Day Avatar answered Oct 14 '22 01:10

Jason Day


On Ubuntu, it's not enough to just change the /etc/localtime file. It seems to read /etc/timezone file, too. It's better follow the instruction to set the time zone properly. In particular, do the following:

$ sudo cp /etc/timezone /etc/timezone.dist
$ echo "Australia/Adelaide" | sudo tee /etc/timezone
Australia/Adelaide
$ sudo dpkg-reconfigure --frontend noninteractive tzdata

Current default time zone: 'Australia/Adelaide'
Local time is now:      Sat May  8 21:19:24 CST 2010.
Universal Time is now:  Sat May  8 11:49:24 UTC 2010.

On my Ubuntu, if /etc/localtime and /etc/timezone are inconsistent, Java seems to read default time zone from /etc/timezone .

like image 35
Liu Zehua Avatar answered Oct 14 '22 01:10

Liu Zehua


I had a similar issue, possibly the same one. However my tomcat server runs on a windows box so the symlink solution will not work.

I set -Duser.timezone=Australia/Sydney in the JAVA_OPTS however tomcat would not recognize that DST was in effect. As a workaround I changed Australia/Sydney (GMT+10:00) to Pacific/Numea (GMT+11:00) so that times would correctly display however I would love to know the actual solution or bug, if any.

like image 3
abarax Avatar answered Oct 14 '22 00:10

abarax