Scala使用JodaTime定义流
我刚刚开始我的scala之旅。我试图定义一个隐式转换,它可以在一段时间内以这种方式迭代每天:Scala使用JodaTime定义流,scala,implicit,Scala,Implicit,我刚刚开始我的scala之旅。我试图定义一个隐式转换,它可以在一段时间内以这种方式迭代每天: for (day <- firstDay until lastDay) { // a lot of interesting code goes here } 实现until方法的方法是什么?该流是否适用于此?或者应该是迭代器还是Seq?还是别的 谢谢我想你在找这样的东西: implicit class DateTimeWithUntil(from: DateTime) { def unt
for (day <- firstDay until lastDay) {
// a lot of interesting code goes here
}
实现until方法的方法是什么?该流是否适用于此?或者应该是迭代器还是Seq?还是别的
谢谢我想你在找这样的东西:
implicit class DateTimeWithUntil(from: DateTime) {
def until(to: DateTime): Stream[DateTime] =
from #:: from.plusDays(1)
}
import org.joda.time._
implicit class DateTimeOps (startDt: DateTime) {
def until(endDt: DateTime) = for(dayNo <- 0 until Days.daysBetween(startDt, endDt).getDays) yield(startDt.plusDays(dayNo))
}
for(day <- new DateTime() until new DateTime().plusDays(10)) println (day)
import org.joda.time_
隐式类DateTimeOps(startDt:DateTime){
def until(endDt:DateTime)=for(day否您当前的代码对to
日期没有任何作用-它返回一个无限的流
。您必须使其在to
日期停止。我尝试了以下操作:from#::(if(DateUtils.isSameDay(from.toDate,to.toDate))Stream.empty-els-to(from.plusDays(1)))但是我认为它不能正常工作。顺便说一句,您可能对joda time的scala包装器感兴趣。Repo有非常旧的提交,所以我不想将其作为依赖项导入,但您可以监视到目前为止的实现。