jodatime期间的错误?
为什么我写的这个测试在 jodatime 1.6.2 中失败了?这是一个错误吗?
@Test
public void testIfJodaTimePeriodsHandlesPeriodTypesOtherThanMinutesAndHours() {
long twentyDaysInMillis = TimeUnit.MILLISECONDS.convert(20, TimeUnit.DAYS);
Period twoWeeks = new Period(twentyDaysInMillis, PeriodType.weeks());
Assert.assertEquals(2, twoWeeks.getWeeks());
// twoWeeks.getWeeks() actually returns 0!!
}
仅供参考,具有所有 periodTypes 的周期仅填写分钟和小时字段,即使传递给构造函数的毫秒数超过 25 小时。这是违反直觉的。
How come this test I wrote fails in jodatime 1.6.2? Is it a bug?
@Test
public void testIfJodaTimePeriodsHandlesPeriodTypesOtherThanMinutesAndHours() {
long twentyDaysInMillis = TimeUnit.MILLISECONDS.convert(20, TimeUnit.DAYS);
Period twoWeeks = new Period(twentyDaysInMillis, PeriodType.weeks());
Assert.assertEquals(2, twoWeeks.getWeeks());
// twoWeeks.getWeeks() actually returns 0!!
}
FYI, Periods with all PeriodTypes only fills in the fields for minutes and hours, even if the millis passed to the constructor amounts to more than 25 hours. This is counterintuitive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是
Period
在 JodaTime 中的工作原理。Period
具有精确字段(小时、分钟、秒、毫秒)和不精确字段(其他)。不精确的字段可能会受到夏令时的影响。也就是说,24 小时的Period
在夏令时边界上可能少于一天或多于一天。因此,需要几毫秒的构造函数仅填充精确的字段。要初始化不精确的字段(不考虑夏令时),您需要:
另请参阅:
期间
javadocThat's how
Period
works in JodaTime.Period
has precise fields (hours, minutes, seconds, milliseconds) and imprecise fields (other). Imprecise fields may be affected by daylight savings. That is,Period
of 24 hours may be less than a day or more than a day on a daylight savings boundary.Therefore, constructors that take milliseconds populate only precise fields. To initialize imprecise fields (without taking daylight savings in account) you need:
See also:
Period
javadoc