使用clj-time按区域设置确定日期格式

时间:2014-09-08 16:04:59

标签: date clojure clj-time

我一直在通过Google搜索这个问题已经有一段时间了,我无法找到clj-time的终极解决方案。我想根据区域设置自动格式化日期,例如this examplehere。我怎么用clj-time做到这一点?

谢谢&干杯

1 个答案:

答案 0 :(得分:4)

使用with-localehttp://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 -

使用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"