3

I have been searching for this for quite a while now through Google and I couldn't find an ultimate solution with clj-time. I want to format a date automatically by the locale, like in this example or here. How would I do this using clj-time?

Thanks & Cheers

RaceCondition
  • 307
  • 2
  • 12

1 Answers1

4

Use with-locale (http://clj-time.github.io/clj-time/doc/clj-time.format.html#var-with-locale)

(require '[clj-time.core :as time] '[clj-time.format :as fmt])
(import '[java.util Locale])

(def custom-formatter (fmt/formatters :rfc822))
(def ja-formatter (fmt/with-locale custom-formatter (Locale. "ja")))
(fmt/unparse ja-formatter (time/date-time 2010 10 3))
> "日, 03 10 2010 00:00:00 +0000"

-UPDATE-

Example of usage of joda-time DateTimeFormat:

(require '[clj-time.core :as time] '[clj-time.format :as fmt])
(import '[java.util Locale])
(import '[org.joda.time.format DateTimeFormat])
(def custom-formatter (DateTimeFormat/longDate))
(def ja-formatter (fmt/with-locale custom-formatter (Locale. "ja")))
(fmt/unparse ja-formatter (time/date-time 2010 10 3))
"2010/10/03"
(def us-formatter (fmt/with-locale custom-formatter (Locale. "us")))
(fmt/unparse us-formatter (time/date-time 2010 10 3))
"October 3, 2010"
edbond
  • 3,921
  • 19
  • 26
  • I tried that and it works with rfc822, but what if I want the format "Monday, October 29, 2014" and the respective formattings in Korea, China, Germany etc.? For now I have gone with this solution: **(.format (SimpleDateFormat/getDateInstance 0 (Locale/US) (t.coerce/to-date clj-time-date))** - and it gives me the correctly formatted dates for the different languages. – RaceCondition Sep 08 '14 at 17:29
  • you may want to build DateTimeFormat from Joda since clj-time is a wrapper around joda-time. http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html – edbond Sep 08 '14 at 17:58