创建dateRange Scala,Joda,Java [英] Creating a dateRange Scala, Joda, Java
问题描述
我花了好几个小时试图使下一段代码工作。
I have spent hours trying to make this next piece of code work.
import org.joda.time.{DateTime, Period}
def dateRange(from: DateTime, to: DateTime, step: Period): Iterator[DateTime] =Iterator.iterate(from)(_.plus(step)).takeWhile(!_.isAfter(to))
val range = {
dateRange(new DateTime(2012, 06, 30).minusYears(5), new DateTime(2000, 06, 30),new Period.months(6))
}
我正在尝试设置一个日期范围数组,该数组从2000年到2012年以6个月为增量逐步完成。我面临的问题是以下错误。
I'm trying to set up a date range array that steps through from 2000 to 2012 in 6 month increments. The problem that I am facing is the following error.
Exception in thread "main" java.lang.IllegalArgumentException: No instant converter found for type: scala.Tuple3
at org.joda.time.convert.ConverterManager.getInstantConverter(ConverterManager.java:165)
at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:169)
at org.joda.time.DateTime.<init>(DateTime.java:241)
at tester.MomentumAlgo$class.$init$(MomentumAlgo.scala:154)
at tester.RunMomentumAlgo$$anon$1.<init>(RunMomentumAlgo.scala:86)
at tester.RunMomentumAlgo$.main(RunMomentumAlgo.scala:86)
at tester.RunMomentumAlgo.main(RunMomentumAlgo.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我似乎与上一个Period.months()部分有关,但是我不知道如何修复它。我不知道Tuple3错误。
I seems to be something to do with the last Period.months() part, however I have no idea how to fix it. The Tuple3 error I have no idea about.
如果有人可以给我一个不同的解决方案,那也会很棒。我想要一份2000至2012年的日期列表,每6个月一次。
If someone can give me a different solution, that would also be great. I want a list of dates from 2000 to 2012, every 6 months.
欢迎提出任何问题。我认为这将是一段很常见的代码,但网上没有太多关于它的信息。
Any questions welcome. I thought this would be a common piece of code, but there isn't much on the net about it.
提前致谢。
推荐答案
解决方法是定义这样的日期:
A work around is to define the dates like this:
val date = new DateTime().withYear(2013).withMonthOfYear(7).withDayOfMonth(16)
然后REPL中的整个序列变为:
The entire sequence in the REPL then becomes this:
scala> import org.joda.time.{DateTime, Period}
import org.joda.time.{DateTime, Period}
scala> def dateRange(from: DateTime, to: DateTime, step: Period): Iterator[DateTime] =Iterator.iterate(from)(_.plus(step)).takeWhile(!_.isAfter(to))
dateRange: (from: org.joda.time.DateTime, to: org.joda.time.DateTime, step: org.joda.time.Period)Iterator[org.joda.time.DateTime]
scala> val from = new DateTime().withYear(2012).withMonthOfYear(6).withDayOfMonth(30).minusYears(5)
from: org.joda.time.DateTime = 2007-06-30T21:46:05.536-07:00
scala> val to = new DateTime().withYear(2000).withMonthOfYear(6).withDayOfMonth(30)
to: org.joda.time.DateTime = 2000-06-30T21:46:26.186-07:00
scala> val range = dateRange(from, to, new Period().withMonths(6))
range: Iterator[org.joda.time.DateTime] = non-empty iterator
scala> range.toList
res4: List[org.joda.time.DateTime] = List(
2000-06-30T21:46:26.186-07:00,
2000-12-30T21:46:26.186-08:00,
2001-06-30T21:46:26.186-07:00,
2001-12-30T21:46:26.186-08:00,
2002-06-30T21:46:26.186-07:00,
2002-12-30T21:46:26.186-08:00,
2003-06-30T21:46:26.186-07:00,
2003-12-30T21:46:26.186-08:00,
2004-06-30T21:46:26.186-07:00,
2004-12-30T21:46:26.186-08:00,
2005-06-30T21:46:26.186-07:00,
2005-12-30T21:46:26.186-08:00,
2006-06-30T21:46:26.186-07:00,
2006-12-30T21:46:26.186-08:00)
此外,我无法重现这一点,如我的评论中所述。似乎REPL和编译器中的行为是不同的。
Also, I wasn't able to reproduce this as noted in my comment. Seems the behavior is different in the REPL and the compiler.
这篇关于创建dateRange Scala,Joda,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!