以小时为单位获取两个日期之间的差额 [英] Get the difference between two dates in hours
本文介绍了以小时为单位获取两个日期之间的差额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算两个joda
日期之间的时差.
I'm trying to calculate the hour difference between two joda
dates.
val d1 = getDateTime1()
val d2 = getDateTime2()
这是我所做的:
# 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
那我该怎么做?
推荐答案
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
对于Period
,没有工厂方法apply
,您应该使用new
创建Period
:
There is no factory method apply
for Period
, you should use new
to create Period
:
val p = new Period(d1, d2, PeriodType.hours())
// org.joda.time.Period = PT672H
如果您使用的是 nscala-时间,则也可以使用方法to
来得到Interval
,然后将其转换为Period
:
If you are using nscala-time you could also use method to
to get Interval
and than convert it to Period
:
d1 to d2 toPeriod PeriodType.hours
// org.joda.time.Period = PT672H
(d1 to d2).duration.getStandardHours
// Long = 672
也尝试sbt clean
.
这篇关于以小时为单位获取两个日期之间的差额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文