2013-07-17 7 views
10

この次のコードを作成するのに何時間も費やしました。dateRangeの作成Scala、Joda、Java

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ヶ月単位で日付範囲の配列を設定しようとしています。私が直面している問題は、次のエラーです。

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ヶ月ごとの日付のリストが必要です。

質問は歓迎します。私はこれがコードの共通部分だと思っていましたが、ネット上にそれほど多くはありません。

ありがとうございます。

+0

はこれを参照してください。 。あなたはREPLの行ごとにこの行を入力していますか? 'scalac'でコンパイルしたときや、' scala'でコードをロードしたときに、scala 2.10.0とJoda Time 1.6でこれを再現することはできません。 – Brian

+0

こんにちはブライアン、私は前にそのスレッドを見ましたが、私はそれを作るために、同じ問題を抱えている他の人々から離れて何かをよく分かりません。私はJoda Time 1.6を実行していますが、Scala 2.9.2です。今すぐ別のシステムでセットアップを試してみてください。私はscala 2.10.2を以前に試みましたが、問題はそこにあるとは思っていません。 – Loooit

+1

同じエラーを返すそのスレッドに記載されているように、このスニペット 'val dt = new DateTime(2013,7,16)'に減らすことができます。 'java.lang.IllegalArgumentException:タイプ:scala.Tuple3 ' – Brian

答えて

12

回避するには、このような日付を定義することです:

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, 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とコンパイラで動作が異なると思われます。

5

DateTimedoesn't have 3つのint引数、引数としてタプル(2012, 06, 30)とそうnew DateTime(2012, 06, 30)通話DateTime(Object)コンストラクタを取るコンストラクタ。ドキュメントには、

という名前のインスタンスが作成されます。これは、datetimeを表すObjectのインスタンスを作成します。

オブジェクトが年表を意味する場合(GregorianCalendarなど)、その年表が使用されます。それ以外の場合は、ISOデフォルトが使用されます。したがって、GregorianCalendarが渡された場合、使用される年表はGJになりますが、年表に渡された日付はISOになります。

認識オブジェクトタイプは、ConverterManagerで定義されReadableInstantStringCalendarDateとが含まれます。 Stringの形式はISODateTimeFormat.dateTimeParser()で記述されています。

驚いたことに、ConverterManagerは、Scalaタプルの処理方法がわかりません。その結果、例外が発生します。

誰かが私に別の解決策を与えることができれば、それも素晴らしいでしょう。私は2000年から2012年までの6ヶ月ごとの日付のリストが必要です。

実際に日付が必要な場合は、使用するのが良いタイプはLocalDateです(ところで、コンストラクタが必要です)。これらの日付の始めにDateTimeが必要な場合は、使用するタイムゾーンについて考える必要があります。

4

ここでは、完全な作業コードを示します。

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) 
    }) 

} 
} 

私は、これは、コンパイラが、それは望んでいたすべてのパラメータを受け取っていなかったもので、私の主な問題は、(ミリ秒すなわちなど)DateTime()機能の後に十分な数字を持っていなかったと思います。 Alexey Romanovが言及したように

これは、希望の範囲の日付を出力し、イテレータとして使用できます。

他人を助ける希望。私は似たような必要とされたヘルプ

4

ため

@Brianおかげで、他の人。ここに私が思い付いたものです:

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

class DateRange(val start: DateTime, val end: DateTime, val step: Period, inclusive: Boolean) extends Iterable[DateTime] { 
    override def iterator: Iterator[DateTime] = new DateRangeIterator 

    class DateRangeIterator extends Iterator[DateTime] { 
     var current = start 

     override def hasNext: Boolean = current.isBefore(end) || (inclusive && current == end) 

     override def next(): DateTime = { 
      val returnVal = current 
      current = current.withPeriodAdded(step, 1) 
      returnVal 
     } 
    } 
} 

使用例:

val startOfDay: DateTime = new DateTime().withTimeAtStartOfDay() 
val endOfDay: DateTime = startOfDay.plusDays(1) 
val dateRange = new DateRange(startOfDay, endOfDay, Period.hours(1), false) 
for (d <- dateRange) println(d) 

出力:http://www.scala-lang.org/node/6982:

2015-03-16T00:00:00.000-05:00 
2015-03-16T01:00:00.000-05:00 
2015-03-16T02:00:00.000-05:00 
2015-03-16T03:00:00.000-05:00 
2015-03-16T04:00:00.000-05:00 
2015-03-16T05:00:00.000-05:00 
2015-03-16T06:00:00.000-05:00 
2015-03-16T07:00:00.000-05:00 
2015-03-16T08:00:00.000-05:00 
2015-03-16T09:00:00.000-05:00 
2015-03-16T10:00:00.000-05:00 
2015-03-16T11:00:00.000-05:00 
2015-03-16T12:00:00.000-05:00 
2015-03-16T13:00:00.000-05:00 
2015-03-16T14:00:00.000-05:00 
2015-03-16T15:00:00.000-05:00 
2015-03-16T16:00:00.000-05:00 
2015-03-16T17:00:00.000-05:00 
2015-03-16T18:00:00.000-05:00 
2015-03-16T19:00:00.000-05:00 
2015-03-16T20:00:00.000-05:00 
2015-03-16T21:00:00.000-05:00 
2015-03-16T22:00:00.000-05:00 
2015-03-16T23:00:00.000-05:00 
関連する問題