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

No way to default date?

Open mtravers opened this issue 1 year ago • 1 comments

I want to parse strings like "November 2024" and default the day of the month to 1. The underlying java classes support this I think (see here) but this isn't brought out into the Clojure API

mtravers avatar Nov 14 '24 21:11 mtravers

Hacked a version that does what I want, probably it should be under an option.

(defn ^java.time.format.DateTimeFormatter formatter
  ([fmt]
   (formatter fmt {}))
  ([fmt {:keys [resolver-style case] :or {case :sensitive}}]
   (let [^DateTimeFormatter fmt
         (cond (instance? DateTimeFormatter fmt) fmt
               (string? fmt) (.. (get-case-formatter case)
                                 (appendPattern fmt)
                                 (parseDefaulting java.time.temporal.ChronoField/DAY_OF_MONTH 1)
                                 toFormatter)
               :else (get jf/predefined-formatters (name fmt)))]
     (cond-> fmt
       resolver-style (.withResolverStyle (get-resolver-style resolver-style))))))

mtravers avatar Nov 14 '24 23:11 mtravers