是否可以一次一天地迭代间隔的开始日期和结束日期之间的时间?使用clj-time
Clojure 库也可以!
问问题
6175 次
4 回答
14
是的。
像这样的东西:
DateTime now = DateTime.now();
DateTime start = now;
DateTime stop = now.plusDays(10);
DateTime inter = start;
// Loop through each day in the span
while (inter.compareTo(stop) < 0) {
System.out.println(inter);
// Go to next
inter = inter.plusDays(1);
}
此外,这里是 Clojure 的 clj-time 的实现:
(defn date-interval
([start end] (date-interval start end []))
([start end interval]
(if (time/after? start end)
interval
(recur (time/plus start (time/days 1)) end (concat interval [start])))))
于 2012-08-19T22:01:42.737 回答
8
这应该有效。
(take-while (fn [t] (cljt/before? t to)) (iterate (fn [t] (cljt/plus t period)) from))
于 2012-08-20T00:12:28.130 回答
2
使用 clj-time,the-interval 是 Joda Interval :
(use '[clj-time.core :only (days plus start in-days)])
(defn each-day [the-interval f]
(let [days-diff (in-days the-interval)]
(for [x (range 0 (inc days-diff))] (f (plus (start the-interval) (days x))))))
于 2012-08-19T22:15:50.033 回答
1
Clj-time 具有periodic-seq 功能。它看起来非常像 Hendekagon 的解决方案。这可以与开始/结束功能相结合,以创建类似:
(defn interval-seq
"Returns sequence of period over interval.
For example, the individual days in an interval using (clj-time.core/days 1) as the period."
[interval period]
(let [end (clj-time.core/end interval)]
(take-while #(clj-time.core/before? % end) (clj-time.periodic/periodic-seq (clj-time.core/start interval) period ))))
于 2014-05-29T01:28:49.287 回答