如何在 Clojure 中实现 TemporalAdjuster

How to implement TemporalAdjuster in Clojure

这里是 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 到 return 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) return 是 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_datejava.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)))