Mr Speaker

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.

Captcha! Please type 'radical' here: *
How did you find this thingo? *