Iterate over dates range (the scala way)

后端 未结 7 1588
逝去的感伤
逝去的感伤 2021-02-04 10:26

Given a start and an end date I would like to iterate on it by day using a foreach, map or similar function. Something like

(DateTime.now to DateTime.now + 5.day         


        
7条回答
  •  滥情空心
    2021-02-04 11:06

    you can use something like that:

     object Test extends App {
       private val startDate: DateTime = DateTime.now()
       private val endDate: DateTime = DateTime.now().plusDays(5)
       private val interval: Interval = new Interval(startDate, endDate)
       Stream.from(0,1)
             .takeWhile(index => interval.contains(startDate.plusDays(index)))
             .foreach(index => println(startDate.plusDays(index)))
     }
    

提交回复
热议问题