我有2 joda.DateTime
s,我用它來創建Duration
。 Duration
然後轉換爲Period
,雖然兩個日期相隔3年,但Period.getDays
,Period.getMonths
和Period.getYears
都返回0.爲什麼?joda期間返回0個月
從斯卡拉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 = [email protected]
scala> formatter.print(duration.toPeriod())
res10: String = 17592h1m
scala> PeriodFormat.getDefault.print(duration.toPeriod)
res11: String = 17592 hours and 1 minute
'toPeriod'的javadoc說的是什麼? –