Iterating the Joda Time Interval

Is it possible to repeat the time between the start and end interval of a date, one day at a time? Using the clj-time library for Clojure will be great too!

+7
source share
4 answers

Yes.

Something like that:

 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); } 

In addition, here is the implementation for 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]))))) 
+13
source

That should work.

 (take-while (fn [t] (cljt/before? t to)) (iterate (fn [t] (cljt/plus t period)) from)) 
+8
source

Using clj-time, the interval is the 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)))))) 
+2
source

Clj time has the function of periodic seq. It is very similar to Hendekagon's solution. This can be combined with begin / end functions to create something like:

 (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 )))) 
+1
source

All Articles