9

I don't live in a country using DST.

  1. I save a future LocalDateTime, Offset from UTC and ZoneId in DB
  2. Government of that zone changes DST rules
  3. That future time comes, I will retrieve the LocalDateTime in that zone

If I apply ZoneId and Offset to get the LocalDateTime in that zone, how does java.time API get this correctly? How does it know that the government changes the rules? Does it fetch update from the internet?

letthefireflieslive
  • 11,493
  • 11
  • 37
  • 61
  • Show the code that you would use. It totally depends on that. A LocalDateTime doesn't have a zone, and if you store the offset from UTC in the database, there is no point in storing the ZoneId - it's either one or the other. If you store the offset from UTC, than you can easily reconstruct the LocalDateTime in your own zone from that and it doesn't matter whether you updated the timezone database in Java or not, and the resulting LocalDateTime will be correct, since DST doesn't get retroactively changed. – Erwin Bolwidt Sep 01 '17 at 02:53
  • 1
    I'm fairly sure when DST does change a new zoneid entry is created. For example there is EST and the newer EDT. I believe both are in the zone db. Unlike Erwin I think you should store the zoneid as it's actually more data since the zonedbs are backward compatible. If you put the offset you are loosing some information. – Adam Gent Sep 01 '17 at 03:07
  • 1
    I meant time zone alias for new DST entry but ideally you store the zoneid. The last time I checked the time libraries know how to retroactively apply the correct offset based on the zoneid. ZoneID is better over offset if you wanted to add a nominal duration of time to some time and then convert that to actual duration: i.e. 3 months is not the same in different tz. – Adam Gent Sep 01 '17 at 03:25
  • Updated. forgot to mention that I'm storing future datetime – letthefireflieslive Sep 01 '17 at 03:36
  • 2
    Obligatory brand new [xkcd](https://xkcd.com/1883/) – Hulk Sep 01 '17 at 12:13
  • In Java 8, it's also possible (although not trivial) to extend/replace the existing timezones, by creating your own `ZoneRulesProvider`: https://stackoverflow.com/a/45595686/7605325 –  Sep 01 '17 at 14:04

1 Answers1

7

Timezone updates usually come with JRE updates. Whenever the timezone of some particular territory is changed, this information is included into the next version of JRE.

Here you can find some information on the timezone data for JRE/JDK.

If updating JRE/JDK is not an option, TZUpdater tool can be used to update only the timezone data without updating JRE/JDK.

All Java date/time APIs that support timezones implicitly use JRE's timezone data, so the only thing you need to worry about is keeping the timezone data in JRE up-to-date.

Aleksei Budiak
  • 871
  • 5
  • 16