JVM and time zones

I have a problem 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 next version of JVM

[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]$ 

System date, say -

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

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

The problem is that this timestamp is returned in EDT, although the server time is in EST. For this reason, the date is returned 1 hour later than it should.

I want to reach Calendar.getInstance().getTime() to return the date in the same time zone as the system.

I searched the forums and found some suggestions that jvm does not read the system time zone correctly. I tried to start tomcat with the -Duser.timezone=EST parameter, but the system saves the returned timestamps in the EDT time zone. Note: trying -Duser.timezone with the non-est option works. The problems seem to be different.

My problem is somehow similar to this SO question . However, I am only trying to get the date in the same time zone as the system, without any special processing.

Can you help?

+8
java timezone
source share
7 answers

I had the same problem. It turns out that for me, Java looked in the file /etc/sysconfig/clock not in the file /etc/localtime . This is for comments for more details.

+4
source share

EST and EDT are very specific, and one of them will always be "wrong" depending on the time of year. Try using the "America / New_York" time zone to get just "what time is it in New York."

eg.

  DateFormat formatterET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); formatterET.setTimeZone(TimeZone.getTimeZone("America/New_York")); String timestamp = formatterET.format(new Date()); 

List of available time zones:

https://calendar.york.ac.uk/en/chcncpt.html#wp1056628

+5
source share

It's pretty simple, add this to your main application method (or servlet context):

 TimeZone.setDefault( TimeZone.getTimeZone("GMT-4") ); 

This sets the time zone for all dates in your system.

+3
source share

I’m not sure if this is really possible in Java, but if so, then this is certainly not a defact way to do what others have said. See here for Oracle notes on this subject, in particular:

Java SE platform time zones are not read from the local or primary operating system (OS), so OS time zone patches will not update the JRE time zone data.

+2
source share

EDT and EST are the same geographic area. But EST is standard time, and it only works in winter (and in some places even in summer), and EDT is daylight saving time. Your problem is probably related to summertime movements, so I will delve into that direction. You can also specify a specific time zone (usually in the Country / City format) by setting the default time zone, but in this case you must be sure that there will not be any collisions with the current time zone of your server and the one you specify by default .

+1
source share

On Linux, this is a little annoying because for a long time, Java (at least Oracle / Sun Java) used an erroneous method (well, I suppose it was less erroneous when it started, but everything changed.)

My best recommendation is to set the TZ environment variable, as this is the first thing it will look for; other places will look (e.g. / etc / sysconfig / clock, / etc / localtime). I have a more detailed post on this at http://distracted-it.blogspot.co.nz/2014/09/dont-let-java-on-linux-determine-its.html , which includes some reference links and a step checks.

-Duser.timezone=Pacific/Auckland may also work, but when I tried it, it is not.

If you install TZ, you must make sure that you install it in the appropriate place. For example, in the WebLogic middleware container, you should set it to setDomainEnv.sh, since WebLogic first deactivates the environment and TZ will not be visible (my post shows how you can verify that the JVM process sees this.)

+1
source share

I had the same problem on an ubuntu server and I cannot find the file / etc / sysconfig / clock.

I decided to use timedatectl to set the time zone.

 sudo timedatectl set-timezone America/New_York 
+1
source share

All Articles