加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

创建dateRange Scala,Joda,Java

发布时间:2020-12-16 09:28:05 所属栏目:安全 来源:网络整理
导读:我花了好几个小时试图使下一段代码工作. 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(n
我花了好几个小时试图使下一段代码工作.

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,30),new Period.months(6))
}

我正在尝试设置一个日期范围数组,以6个月为增量从2000年到2012年逐步完成.我面临的问题是以下错误.

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错误我不知道.

如果有人能给我一个不同的解决方案,那也会很棒.我想要一份2000至2012年的日期列表,每6个月一次.

欢迎任何问题.我认为这将是一个常见的代码片段,但网上没有太多关于它的内容.

提前致谢.

解决方法

解决方法是定义这样的日期:

val date = new DateTime().withYear(2013).withMonthOfYear(7).withDayOfMonth(16)

然后REPL中的整个序列变为:

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

scala> def dateRange(from: 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和编译器中的行为是不同的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读