使用clj-time根据本地设置确定日期格式

3
我已经通过Google搜索了相当长时间,但我使用clj-time找不到一个终极解决方案。我想要根据地区自动格式化日期,就像在这个例子这里一样。我应该如何使用clj-time实现这个功能?谢谢!
1个回答

4

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

-更新-

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"

我尝试了一下,它可以用rfc822格式,但是如果我想要"2014年10月29日星期二"这种格式,以及在韩国、中国、德国等地对应的格式怎么办?目前我采用了这个解决方案:**(format (SimpleDateFormat/getDateInstance 0 (Locale/US) (t.coerce/to-date clj-time-date)))** - 它给我提供了不同语言正确格式化的日期。 - RaceCondition
你可能想要从Joda构建DateTimeFormat,因为clj-time是围绕joda-time的包装器。http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html - edbond

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接