如何使用Scala中的Joda在DateTime中进行长期类型



如何使用Scala中的Joda在DateTime中转换长时间?

val a = 1234526278L
val b: DateTime = 1234526278L.DateTime

日期时间的创建是微不足道的(假设给定的长度代表"来自epoch的毫秒"):

val b = new DateTime(a)

但我假设作者想知道如何获得所需的语法,这可以通过以下代码实现:

class LongExtension(private val l: Long) extends AnyVal {
  def toDateTime = new DateTime(l)
}
implicit def toExtension(l: Long) = new LongExtension(l)

现在,如果隐式转换在范围中可用,则可以使用以下Santaxt:

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

最新更新