14

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?

Community
  • 1
  • 1
Andrei
  • 1,613
  • 3
  • 16
  • 36
  • Keep in mind if you are only looking for a date object with the current time you can call new Date() instead of using Calendar. You will still need to follow Mauricio's advice by setting the default timezone however. – jpredham Aug 15 '11 at 14:48
  • You probably already know, but EDT and EST are the same timezone with/without DST. August is generally EDT, so _practically_ there is no EST in August in the US. http://en.wikipedia.org/wiki/Eastern_Time_Zone – Nivas Aug 15 '11 at 14:59
  • Yes, I'm thinking the issue might be related to that, since -Duser.timezone=AET for example works. However I'm looking for the permanent fix to this problem rather than doing workarounds. – Andrei Aug 15 '11 at 15:25

7 Answers7

10

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

mlathe
  • 2,375
  • 1
  • 23
  • 42
5

EST and EDT are very specific, and one of them will alway be "wrong" depending on the time of year. Try a timezone of "America/New_York" to get simply "what the time is in New York".

E.g.

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

    String timestamp = formatterET.format(new Date());

Useful time zones list:

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

Brian
  • 6,391
  • 3
  • 33
  • 49
  • Hi and thank you for answering. Although I'm aware that some workarounds are possible, I would like to fix the real issue. My question, in summary is : "how to make JVM read the system's timezone and use that by default" – Andrei Aug 15 '11 at 14:46
  • I see what you mean; perhaps consider this a workaround which should have the same effect if you can't find a more definitive answer. I will be interested to see if there is an answer, I don't know of such a setting at present. – Brian Aug 15 '11 at 15:14
4

This is quite simple, add this at your app main method (or servlet context):

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

This sets the timezone for all dates in your system.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • N.B. Be aware this method will only remain in effect for the lifetime of the JVM instance e.g. will not survive restarts of Tomcat. – jpredham Aug 15 '11 at 14:42
  • Yep, that's why you should add it to a servlet context listener or something like that, making sure it runs whenever your app is loaded. – Maurício Linhares Aug 15 '11 at 14:43
  • Hi and thank you for answering. Although I'm aware that some workarounds are possible, I would like to fix the real issue. My question, in summary is : "how to make JVM read the system's timezone and use that by default" – Andrei Aug 15 '11 at 14:47
  • fyi - I just learned that `TimeZone.setDefault()` behaves differently depending on whether a SecurityManager is present (like with Tomcat, etc.) in which case the effect of setting the default timezone has thread local scope, and the rest of the JVM won't see it. See: http://stackoverflow.com/a/9891971/516910 – broc.seib Apr 02 '15 at 02:16
2

I'm not sure if this is actually possible in Java, but if so it's certainly not the defacto way of doing things as others have said. See here for Oracle's notes on the matter, in particular:

The Java SE platform's timezone data is not read from the local or host operating system (OS), so OS timezone patches will not update the JRE software's timezone data.

MarkH
  • 31
  • 4
2

I had the same issue on ubuntu server, and can't find /etc/sysconfig/clock file.

I solved it use the timedatectl to set the timezone.

sudo timedatectl set-timezone America/New_York
Green Lei
  • 3,150
  • 2
  • 20
  • 25
1

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

My best recommendation would be to set the TZ environment variable, as this is the first thing it will look for; the other placesit will look (eg. /etc/sysconfig/clock, /etc/localtime) are faulty. I have a more detailed post about it at http://distracted-it.blogspot.co.nz/2014/09/dont-let-java-on-linux-determine-its.html, which includes some background references and a verification step.

-Duser.timezone=Pacific/Auckland may also work, but when I tried it, it didn't.

If setting TZ, you should make sure you set it in an appropriate place. For example, in the WebLogic middleware container, you should set it in setDomainEnv.sh, as WebLogic will sanitize the environment first and TZ won't be seen (my post shows how you can verify that a JVM process is seeing it.)

Cameron Kerr
  • 1,725
  • 16
  • 23
1

EDT & EST - it's the same geographical zone. But EST is a standard time and it works only in winter (and in some places even in summer), and EDT is a daylight saving counterpart. Your issue is probably related to daylight saving movements, so I'd dig into this direction. You can also specify a particular timezone (usually in Country/City format) by setting the default timezone, but in this case you should be sure there won't be any clashes with your server current timezone and the one you specify as default.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
  • Sorry, I don't think it matters what's the difference between them. All I want is JVM to report the same time/timezone as the system's clock. – Andrei Aug 15 '11 at 15:00