0

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?

wthigo
  • 115
  • 4
alex
  • 23
  • 2
  • What is `ZoneId.systemDefault()` returning? – Joe C Mar 14 '18 at 08:09
  • 1
    At a guess, your JVM doesn't attempt to parse anything after the first comma. For example `TZ="EST+5EDT,I'm a little teapot"` produces the same output as `TZ="EST+5EDT,M3.2.0/02:00,M11.1.0/02:00"`. – Andy Turner Mar 14 '18 at 08:26
  • 1
    @JoeC ZoneId.systemDefault() returns GMT-05:00 – alex Mar 14 '18 at 08:29
  • @AndyTurner It seems like JVM bug, Do you have any ideas to go around it ? – alex Mar 14 '18 at 08:31
  • "It seems like JVM bug" whether it is a bug depends upon whether the JVM is intended to support this timezone format - I've looked, and can't find anything which says it should. You can write something which parses the string yourself, and then create an instance of [`SimpleTimeZone`](https://docs.oracle.com/javase/8/docs/api/java/util/SimpleTimeZone.html) (or just create a "fixed" instance, and set it as your default on startup). – Andy Turner Mar 14 '18 at 08:33
  • But by all means file it as a bug with Oracle if you think it is; let them decide. – Andy Turner Mar 14 '18 at 08:34
  • 1
    Looking [here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/TimeZone.java#669), it looks as though Java only supports fixed-offset custom time zones if the time zone isn't one it recognizes. – Andy Turner Mar 14 '18 at 08:37
  • Why do you want to define your own time zone rules? What’s wrong with America/New_York time zone? – Ole V.V. Mar 14 '18 at 10:19
  • Noting wrong but installed linux env has only one options to set timezone(exporting TZ variable in Posix format), so OS will not able to understand America/New_York format – alex Mar 14 '18 at 10:37
  • @AndyTurner thank you, it looks like there are no ways to learn Java understand posix timezone without spike – alex Mar 14 '18 at 10:45

1 Answers1

1

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.

wthigo
  • 115
  • 4