0
jodaをスカラで使用してDateTimeのLong型を変換するにはどうすればよいですか?日付時刻のどのようにスカラのjodaとDateTimeでロングタイプを変換する?
val a = 1234526278L
val b: DateTime = 1234526278L.DateTime
jodaをスカラで使用してDateTimeのLong型を変換するにはどうすればよいですか?日付時刻のどのようにスカラのjodaとDateTimeでロングタイプを変換する?
val a = 1234526278L
val b: DateTime = 1234526278L.DateTime
作成は簡単です(長い「エポックからのミリ秒」を表し、与えられたと仮定した場合):
val b = new DateTime(a)
をしかし、私は、著者は、所望の構文を取得する方法を知りたいと思ったと仮定し、これはとachivedすることができます以下のコード:
class LongExtension(private val l: Long) extends AnyVal {
def toDateTime = new DateTime(l)
}
implicit def toExtension(l: Long) = new LongExtension(l)
今暗黙の変換がスコープで利用可能である場合、以下syntaxtを使用することができる。
val c = a.toDateTime
scala> import org.joda.time.DateTime
import org.joda.time.DateTime
scala> val b = new DateTime(a)
b: org.joda.time.DateTime = 1970-01-15T12:25:26.278+05:30
implicitsを扱う場合は、 '暗黙のdef dt(l:Long)= new DateTime(l);を実行します。 val c:DateTime = 12345l' – Dima