Skip to content

Instantly share code, notes, and snippets.

@henryw374
Last active December 15, 2021 11:07
Show Gist options
  • Save henryw374/2827fd6391e3ed9636ead86db06db821 to your computer and use it in GitHub Desktop.
Save henryw374/2827fd6391e3ed9636ead86db06db821 to your computer and use it in GitHub Desktop.
java.time offset zonedatetime
; demonstrating that offset names such as EST, CEST etc are not always parsed properly into ZonedDateTime.
; EST, CEST are defined as fixed offsets from UTC so are not ambiguous in that respect, although apparently the same abbreviation
; is used more than once for the same thing in some cases! Ideally they could be parsed to OffsetDateTime,
; but I can't find a way to get that working. Comment if you know how ;-)
; old parse api
(->
(SimpleDateFormat. "M/d/y, H:m z")
(.parse "3/26/2020, 14:00 EST"))
;; or even
(Date. "3/26/2020, 14:00 EST")
;; ... correctly returns #inst"2020-03-26T19:00:00.000-00:00"
; new & improved java.time
(-> (java.time.ZonedDateTime/parse
"3/26/2020, 14:00 EST"
(java.time.format.DateTimeFormatter/ofPattern "M/d/y, H:m z"))
(.toInstant)
(java.util.Date/from))
; results in #inst"2020-03-26T18:00:00.000-00:00". wrong!
; see https://stackoverflow.com/a/45745127/1700930 for more info
; but tl;dr I don't see DateTimeFormatterBuilder.appendZoneText as a great solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment