Creating a dateRange Scala, Joda, Java

后端 未结 5 546
生来不讨喜
生来不讨喜 2020-12-16 01:34

I have spent hours trying to make this next piece of code work.

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


def dateR         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-16 02:08

    Ok, Here is the complete working code.

    import org.joda.time.{Period, DateTime}
    
    object runme {
    
      def main(args:Array[String]) {
    
      def dateRange(from: DateTime, to: DateTime, step: Period): Iterator[DateTime]
      =Iterator.iterate(from)(_.plus(step)).takeWhile(!_.isAfter(to))
    
      val range = { dateRange(new DateTime(2000, 06, 30,0,0,0,0).minusYears(5) ,new DateTime(2013, 06, 30,0,0,0,0),new Period(0,6,0,0,0,0,0,0))}
    
      range.foreach(u => { 
        print(u.getYear)
        print(u.getMonthOfYear)
        println(u.getDayOfMonth)
      })
    
     }
    }
    

    I think my main problem was not having enough numbers after the DateTime() functions (ie the milliseconds etc.) this meant the compiler wasn't receiving all the parameters that it wanted. As mentioned by Alexey Romanov

    This then prints the dates for a desired range, and can be used as an iterator.

    Hope that helps others.

    Thanks @Brian and others for the Help

提交回复
热议问题