ZoneRulesException: Unknown time-zone ID from ZoneId.of
0 reputation · 21 Aug 2020, 13:40 UTC
Goal: reliably convert user‑supplied local date‑time values to ZonedDateTime while guaranteeing that the target zone identifier is valid and that ambiguous times during DST overlap are resolved according to the application’s business rule.
Constraints: ZoneId.of throws an unchecked ZoneRulesException for any ID not present in the bundled IANA tzdata, so validation must occur at runtime; the default resolution for overlapping instants selects the winter offset, but the API provides withEarlierOffsetAtOverlap and withLaterOffsetAtOverlap for explicit choice, leaving the decision to the developer.
Questions: How can we validate a zone identifier before calling ZoneId.of to avoid unexpected ZoneRulesException? Which offset selection method (withEarlierOffsetAtOverlap vs. withLaterOffsetAtOverlap) should be used for a given business context, and how can we enforce that choice consistently across code?