clojure.java-time icon indicating copy to clipboard operation
clojure.java-time copied to clipboard

When using `java-time.api/formatter`: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

Open devurandom opened this issue 8 months ago • 2 comments

(jt/instant (jt/formatter "YYYYMMdd'T'HHmmssX" {:resolver-style :lenient
                                                :case           :insensitive})
            "20240611T131542Z")
; throws:
; java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
;                         java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {MonthOfYear=6, WeekBasedYear[WeekFields[SUNDAY,1]]=2024, DayOfMonth=11, OffsetSeconds=0},ISO resolved to 13:15:42 of type java.time.format.Parsed
;                          clojure.lang.ExceptionInfo: Conversion failed

On the other hand:

(jt/instant (-> (.toFormatter (doto (DateTimeFormatterBuilder.)
                                (.parseLenient)
                                (.parseCaseInsensitive)
                                (.appendValue ChronoField/YEAR 4)
                                (.appendValue ChronoField/MONTH_OF_YEAR 2)
                                (.appendValue ChronoField/DAY_OF_MONTH 2)
                                (.appendLiteral "T")
                                (.appendValue ChronoField/HOUR_OF_DAY 2)
                                (.appendValue ChronoField/MINUTE_OF_HOUR 2)
                                (.appendValue ChronoField/SECOND_OF_MINUTE 2)
                                (.appendOffset "+HHMMss" "Z")))
                (.withZone (ZoneId/of "UTC")))
            "20240611T131542Z")
;=> #object[java.time.Instant 0x52b9aba9 "2024-06-11T13:15:42Z"]

I would have expected both formatters to be (more or less) equivalent.

(My aim is to build a formatter and parser for the ISO 8601 "basic" format, as opposed to the "extended" format containing dashes and colons. All values need to be in the UTC timezone.)

devurandom avatar Jun 11 '24 14:06 devurandom