Skip to content

Instantly share code, notes, and snippets.

@Fristi
Created February 23, 2016 15:58
Show Gist options
  • Select an option

  • Save Fristi/d242c099ec78789bdcf6 to your computer and use it in GitHub Desktop.

Select an option

Save Fristi/d242c099ec78789bdcf6 to your computer and use it in GitHub Desktop.
Get week intervals for a specified period
import org.joda.time.{Interval, Period, DateTime}
object Main {
def getWeeks(startDate: DateTime, endDate: DateTime) = {
val weekPeriod = new Period().withWeeks(1)
def loop(current: Interval, weeks: Seq[Interval]): Seq[Interval] = {
if(current.getEnd.isBefore(endDate)) {
loop(new Interval(current.getStart.plus(weekPeriod), weekPeriod), weeks :+ current)
} else {
weeks
}
}
loop(new Interval(startDate, weekPeriod), Seq.empty)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment