是否可以迭代间隔的开始日期和结束日期之间的时间,一次一天?使用Clojure的clj-time库也很好!
发布于 2012-08-20 06:01:43
是啊。
如下所示:
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-20 08:12:28
这应该是可行的。
(take-while (fn [t] (cljt/before? t to)) (iterate (fn [t] (cljt/plus t period)) from))发布于 2012-08-20 06:15:50
使用clj-time,- Interval是Joda间隔:
(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))))))https://stackoverflow.com/questions/12030322
复制相似问题