[英]joda Period returns 0 months
我有2个joda.DateTime
,用于创建Duration
。 然后,将Duration
转换为Period
,尽管两个日期相隔3年,但是Period.getDays
, Period.getMonths
和Period.getYears
都返回0。为什么?
来自Scala REPL的示例,但应该易于理解/转换为Java(如果需要,我可以转换)
import org.joda.time.format.PeriodFormat
import org.joda.time.{DateTime, Period}
import org.joda.time.Duration
import org.joda.time.format.PeriodFormatterBuilder
scala> val start = DateTime.now
start: org.joda.time.DateTime = 2017-02-22T16:09:13.131Z
scala> val end = start.plusYears(2).plusMinutes(1).plusDays(3)
end: org.joda.time.DateTime = 2019-02-25T16:10:13.131Z
scala> val duration = new Duration(start, end)
duration: org.joda.time.Duration = PT63331260S
scala> duration.getMillis
res0: Long = 63331260000
scala> duration.getStandardDays
res1: Long = 733
scala> duration.getStandardHours
res2: Long = 17592
scala> duration.getStandardMinutes
res3: Long = 1055521
scala> duration.getStandardSeconds
res4: Long = 63331260
scala> val period = duration.toPeriod()
period: org.joda.time.Period = PT17592H1M
scala> period.getDays
res5: Int = 0
scala> period.getHours
res6: Int = 17592
scala> period.getMinutes
res7: Int = 1
scala> period.getSeconds
res8: Int = 0
scala> period.getMillis
res9: Int = 0
scala> val formatter = new PeriodFormatterBuilder()
.appendMonths().appendSuffix(" month", " months").appendSeparator(" ")
.appendDays().appendSuffix(" day", " days").appendSeparator(" ")
.appendHours().appendSuffix("h")
.appendMinutes().appendSuffix("m")
.appendSeconds().appendSuffix("s")
.toFormatter
formatter: org.joda.time.format.PeriodFormatter = org.joda.time.format.PeriodFormatter@3096d00d
scala> formatter.print(duration.toPeriod())
res10: String = 17592h1m
scala> PeriodFormat.getDefault.print(duration.toPeriod)
res11: String = 17592 hours and 1 minute
您正在调用AbstractDuration.toPeriod
:
使用标准期间类型和ISO年表将这个期间转换为一个Period实例。
仅使用期间类型中的精确字段。 因此,将仅使用周期中的小时,分钟,秒和毫秒字段。 不会填充年,月,周和日字段。
Duration
不会“知道”其起点和终点,因此您不能期望填充诸如月之类的可变长度值。
要获取从start
到end
之间的时间,您应该使用:
new Period(start, end)
...可能带有PeriodType
来说明您想要的单位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.