Andrei
Andrei

Reputation: 1635

JVM and timezones

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?

Upvotes: 14

Views: 43374

Answers (7)

Green Lei
Green Lei

Reputation: 3412

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

Upvotes: 2

Cameron Kerr
Cameron Kerr

Reputation: 1875

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.)

Upvotes: 1

mlathe
mlathe

Reputation: 2385

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

Upvotes: 10

MarkH
MarkH

Reputation: 31

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.

Upvotes: 2

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15308

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.

Upvotes: 1

Brian
Brian

Reputation: 6450

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

Upvotes: 5

Maurício Linhares
Maurício Linhares

Reputation: 40323

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.

Upvotes: 4

Related Questions