【问题标题】:How does DateTime.now.hour(0) get a new DateTime in nscala-time?DateTime.now.hour(0) 如何在 nscala-time 中获取新的 DateTime?
【发布时间】:2015-06-11 01:46:46
【问题描述】:

我使用来自 https://github.com/nscala-time/nscala-time 的 nsacala-time 包。

import com.github.nscala_time.time.Imports._
DateTime.now.hour(0)
res0: org.joda.time.DateTime = 2015-06-11T22:33:52.266+08:00

将获得一个新的 DateTime 对象。 但是在源代码https://github.com/nscala-time/nscala-time/blob/master/src/main/scala/com/github/nscala_time/time/RichDateTime.scala 中,它没有带有签名(Int)的小时函数。 似乎 withHour(hour: Int) 做了确切的工作。

【问题讨论】:

    标签: scala datetime jodatime nscala-time


    【解决方案1】:

    无处不在的隐式转换(又名黑魔法,也就是为什么每个人都讨厌 Scala)!!!

    1. hour 返回一个org.joda.time.DateTime.Property

    2. Import._ 导入 RichDateTimeProperty

    3. RichDateTimeProperty 定义了一个 apply 方法,默认情况下在 Scala 中执行 something() 如果 something 不是方法被转换为 something.apply(),所以最后这个方法通过 2 个隐式转换调用:

      def apply(value: Int): DateTime = underlying.setCopy(value)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2013-11-26
      相关资源
      最近更新 更多