【问题标题】:Convert Clojure #inst instant in time to Joda time with clj-time使用 clj-time 将 Clojure #inst 即时转换为 Joda 时间
【发布时间】:2017-11-13 20:51:39
【问题描述】:

使用clj-time 库将#inst "2017-01-01T12:00:00" 等Clojure 时间瞬间解析为Joda 时间的正确方法是什么?

【问题讨论】:

    标签: parsing clojure jodatime clj-time


    【解决方案1】:

    如果你有一个java.util.Date 对象:

    (type #inst "2017-01-01T12:00:00+02:00")
    ;;=> java.util.Date
    

    clj-time 有一个 clj-time.coerce 命名空间和一个 from-date 函数,该函数将 java.util.Date 对象作为输入。示例:

    (require '[clj-time.coerce :as c])
    
    ((juxt type str) (c/from-date #inst "2017-01-01T12:00:00"))
    ;;=> [org.joda.time.DateTime "2017-01-01T12:00:00.000Z"]
    

    【讨论】:

      【解决方案2】:

      clj-time 现在还提供了一个数据阅读器来自动将 EDN 中的日期强制转换为/从 joda 日期时间:clj-time EDN support

      (clojure.edn/read-string {:readers clj-time.coerce/data-readers}
                               "#clj-time/date-time \"2019-07-10T06:00:00.000Z\"")
      ==>
      #clj-time/date-time "2019-07-10T06:00:00.000Z"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 2015-02-11
        • 2013-10-22
        • 1970-01-01
        • 2014-02-11
        相关资源
        最近更新 更多