如何在 Joda 中将持续时间转换为期间?

How to convert a Duration to Period in Joda?

有人可以解释为什么以下测试失败吗?

@Test
public void testDuration() {
    Duration duration = Duration.standardDays(1);
    assertTrue(duration.getStandardMinutes() == 1440); //OK
    assertTrue(duration.toPeriod().getMinutes() == 1440); //NOK. result = 0
    assertTrue(new Period(duration.getMillis()).getMinutes() == 1400); //NOK. result = 0
}

JodaTime中的一个Period表示一个"set of duration field values"。因此,getMinutes()getHours()、...方法返回这些字段的值,而不是计算分钟、小时...

此外,从 Duration 的转换根据 PeriodType 和年表设置字段。

来自API (http://joda-time.sourceforge.net/apidocs/org/joda/time/ReadableDuration.html#toPeriod%28%29):

Period toPeriod()

Converts this duration to a Period instance using the standard period type and the ISO chronology.

Only precise fields in the period type will be used. Thus, only the hour, minute, second and millisecond fields on the period will be used. The year, month, week and day fields will not be populated.

If the duration is small, less than one day, then this method will perform as you might expect and split the fields evenly. If the duration is larger than one day then all the remaining duration will be stored in the largest available field, hours in this case.

这意味着,由于 Duration 的一天恰好是 24 小时,所有信息都存储在小时字段中,分钟保持为 0。

请参阅 this question 了解 IntervalDurationPeriod 之间差异的详细解释。

here

A period of 1 day is not equal to a period of 24 hours, nor 1 hour equal to 60 minutes. This is because periods represent an abstracted definition of a time period (eg. a day may not actually be 24 hours, it might be 23 or 25 at daylight savings boundary).