【问题标题】:Clojure (cljc.java-time) - Casting error during Number of Days between to DatesClojure (cljc.java-time) - 到日期之间的天数期间的转换错误
【发布时间】:2021-08-04 14:12:11
【问题描述】:

我在理解如何处理此错误时遇到了一些麻烦。或者我应该在哪里寻找示例/文档。我倾向于 cljc.java-time 库以便能够在 Clojure 和 ClojureScript 中使用它。

(ns cljexplore.core
  (:require [cljc.java-time.local-date :as ld])
  (:require [cljc.java-time.period :as period])
  (:require [cljc.java-time.duration :as duration]))

(def bbday (ld/parse "2021-11-12"))
(def today (ld/now))
(defn -main
  [& args]
  (let [dur (period/between today bbday)]
  (println (period/of-days dur))))

当我尝试运行此程序时,我收到以下错误: 类 java.time.Period 不能转换为类 java.lang.Number(java.time.Period 和 java.lang.Number 在加载器“bootstrap”的模块 java.base 中)

不确定从这里到哪里可以使用此库简单计算两个日期之间的距离。可以朝正确的方向轻推。

【问题讨论】:

    标签: date clojure clojurescript java-time


    【解决方案1】:

    也许,这就是你要找的:

     (require '[cljc.java-time.temporal.chrono-unit :as chrono-unit])
     (chrono-unit/between chrono-unit/days today bbday)
     => 100
    

    【讨论】:

    • 嗯,然后在 (C:\Users\buddy\AppData\Local\Temp\form-init12575945163277844101.clj:1:108) 处编译时出现语法错误 (UnsupportedTemporalTypeException)。不支持的单位:秒
    • 更新了我的答案以更好地反映解决方案
    猜你喜欢
    • 2011-04-17
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2016-08-13
    相关资源
    最近更新 更多