Mr Speaker


Warning: count(): Parameter must be an array or an object that implements Countable in /home/mrspeak/public_html/wp-content/themes/stuartspeaker/functions.php on line 24

Warning: count(): Parameter must be an array or an object that implements Countable in /home/mrspeak/public_html/wp-content/themes/stuartspeaker/functions.php on line 27

Warning: count(): Parameter must be an array or an object that implements Countable in /home/mrspeak/public_html/wp-content/themes/stuartspeaker/functions.php on line 29

Scala date range

Google it: "Scala date range". The results are... unhelpful. The top result (a Stack Overflow link, obviously) hints at a workable solution. Here's my implementation of it:

import org.joda.time.{DateTime, Period}

def dateRange(from: DateTime, to: DateTime, step: Period): Iterator[DateTime] =
  Iterator.iterate(from)(_.plus(step)).takeWhile(!_.isAfter(to))

To use it, provide a "from" date, a "to" date and a joda time period:

val range = dateRange(
    DateTime.now().minusYears(5),
    DateTime.now(),
    Period.months(6))

Which gives you an iterator starting 5 years ago, containing every date 6 months apart, up until now. An iterator is part of scala collections - so you can toList it or map/filter etc.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/mrspeak/public_html/wp-content/themes/stuartspeaker/functions.php on line 33
Captcha! Please type 'radical' here: *
How did you find this thingo? *