ali haider ali haider - 1 month ago 15
Java Question

DST hour from local timezone DB using JDK8

I am using JDK8 zonedatetime to get the DST rules e.g:

ZoneId zoneIdTmp = ZoneId.of("America/New_York");
ZoneRules rules = zoneId.getRules();
ZoneOffsetTransition nextTransition = rules.nextTransition(Instant.now());


The output I am getting is as follows:

2016-11-06T01:00-05:00[America/New_York]


DST switches at 2am (back to 1am) on Nov 6th. I am assuming the 1:00 am time in the output above points to the new time after the DST change and the 5:00 points to the new offset from UTC. I have checked my local timezone database

zdump -v /usr/share/zoneinfo/America/New_York


the output there displays the (approx) 2am time

Sun Nov 6 05:59:59 2016 UT = Sun Nov 6 01:59:59 2016 EDT isdst=1 gmtoff=-14400


Instead of adding any logic myself from the JDK output, is there a way for me to get the 2am time from the ZoneDateTime (or a reliably working logic that works across different zone IDs).

Answer

How did you output the rule? With toString()? If you use the getters there is all kind of information which looks like what you are after, especially ZoneOffsetTransition.getOffsetBefore() and getOffsetAfter() in combination with getInstant() or getDateTimeAfter().