gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:11:17 28 4
gpt4 key购买 nike

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

@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 之间的区别有很好的解释。

关于java - 如何在 Joda 中将持续时间转换为期间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347521/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com