我正在尝试计算两个joda日期之间的小时差异.
val d1 = getDateTime1()
val d2 = getDateTime2()
Run Code Online (Sandbox Code Playgroud)
这是我做的:
# attemp1
val hours = Hours.hoursBetween(d1, d2) // error - types mismatch
# attempt2
val p = Period(d1, d2, PeriodType.hours()) // error - there is such a constructor
p.getHours
Run Code Online (Sandbox Code Playgroud)
那我该怎么做?
getDateTime1应该是的类型org.joda.time.DateTime.
这很好吃:
val d1: org.joda.time.DateTime = DateTime.now
val d2: org.joda.time.DateTime = DateTime.nextMonth
val hours = Hours.hoursBetween(d1, d2)
// org.joda.time.Hours = PT672H
Run Code Online (Sandbox Code Playgroud)
没有工厂方法apply的Period,你应该使用new创建Period:
val p = new Period(d1, d2, PeriodType.hours())
// org.joda.time.Period = PT672H
Run Code Online (Sandbox Code Playgroud)
如果您使用的是nscala-time,您还可以使用方法to获取Interval并将其转换为Period:
d1 to d2 toPeriod PeriodType.hours
// org.joda.time.Period = PT672H
(d1 to d2).duration.getStandardHours
// Long = 672
Run Code Online (Sandbox Code Playgroud)
也试试sbt clean.
| 归档时间: |
|
| 查看次数: |
7654 次 |
| 最近记录: |