alex
alex

Reputation: 23

Java time ignores daylight saving rules

I am exporting TZ variable in POSIX format to set timezone on Linux. For instance:

export TZ="EST+5EDT,M3.2.0/02:00,M11.1.0/02:00"

Linux date command returns:

Wed Mar 14 03:47 EDT 2018

Java ZonedDateTime.now() returns:

2018-03-14T02:47:36.808[GMT-05:00]

It seems that Java does not take into account DST rule. What can be wrong?

Upvotes: 0

Views: 601

Answers (1)

wthigo
wthigo

Reputation: 115

I'm not sure what linux version you're using, but I've tested in Red Hat 4.4 and it accepts IANA's names:

export TZ=America/New_York
date

And the output is:

Qua Mar 14 08:37:25 EDT 2018

I've also checked some articles on web and all examples use names like "America/New_York", "Europe/London" and so on.

But anyway, if your linux version doesn't work with this, it's better to change your code to not use the JVM default timezone:

ZonedDateTime.now(ZoneId.of("America/New_York"));

Actually, I think it's better to use a specific timezone, because the default can be changed at runtime, for any application running in the same JVM. Even if you have control over what the applications do, some infrastructure/environment maintainance can change that, either on purpose or by accident - it happened to me once, which made me start using explicit timezones names everywhere.

And always prefer IANA's names, in the format Continent/Region. Names like EST+5EDT are fixed, in the sense that they represent just an offset (in this case, GMT-05:00), without any Daylight Saving rules. Only names like America/New_York contains DST rules.

Upvotes: 1

Related Questions