Clojure新手,在这里。
我正在尝试在Clojure中实现TemporalAdjuster。我有以下内容:
(ns pto-calculator.logic.pay-periods
(:require [java-time :as j]))
(def next-pay-period
(reify java.time.temporal.TemporalAdjuster
(adjustInto [this temporal]
(let [local-date (java.time.LocalDate/from temporal)
day-of-month (.getDayOfMonth local-date)]
(if (< 14 day-of-month)
(j/plus local-date (j/days (- 14 day-of-month)))
(j/adjust local-date :last-day-of-month))))))
(defn get-next-pay-period [date]
(j/adjust date next-pay-period))
我称之为:
(ns pto-calculator.core
(:require [pto-calculator.logic.pay-periods :as p]
[java-time :as j])
(:gen-class))
(defn -main
[& args]
(p/get-next-pay-period j/local-date))
今天是3月2日,所以我希望get-next-pay-period
能够在3月14日回归,但是,我得到一个例外:
Caused by: java.lang.ClassCastException: java_time.local$local_date cannot be cast to java.time.temporal.Temporal
at java_time.adjuster$adjust.invokeStatic(adjuster.clj:64)
at java_time.adjuster$adjust.doInvoke(adjuster.clj:40)
at clojure.lang.RestFn.invoke(RestFn.java:425)
at pto_calculator.logic.pay_periods$get_next_pay_period.invokeStatic(pay_periods.clj:19)
at pto_calculator.logic.pay_periods$get_next_pay_period.invoke(pay_periods.clj:18)
我的困惑是:(j/local-date)
返回java.time.LocalDate
的一个实例,这是一个Temporal
(根据the docs)。那么为什么这不起作用呢?
我也尝试过:
(defn get-next-pay-period [^java.time.temporal.Temporal date]
...
但在那种情况下,我收到此错误:
java_time.local$local_date cannot be cast to java.time.temporal.Temporal
java_time.local$local_date
和java.time.LocalDate
有区别吗?
您没有在核心命名空间中调用java-time/local-date
。您正在将函数传递给get-next-pay-period
。
代替:
(defn -main
[& args]
(p/get-next-pay-period j/local-date))
尝试:
(defn -main
[& args]
(p/get-next-pay-period (j/local-date)))