Given a range, getting all dates within that range in Scala

前端 未结 6 1868
礼貌的吻别
礼貌的吻别 2021-02-04 03:29

I need to make a function in scala that, given a range of dates, gives me a list of the range. I am relatively new in Scala and I am not able to figure out how to write the righ

6条回答
  •  爱一瞬间的悲伤
    2021-02-04 03:59

    Try this

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

    To generate every date, you can set the step to 1 day like

    val range = dateRange(
    ,
    ,
    Period.days(1))
    

提交回复
热议问题