jodatime期间的错误?

发布于 2024-09-29 07:43:20 字数 480 浏览 3 评论 0原文

为什么我写的这个测试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-10-06 07:43:20

这就是 Period 在 JodaTime 中的工作原理。

Period 具有精确字段(小时、分钟、秒、毫秒)和不精确字段(其他)。不精确的字段可能会受到夏令时的影响。也就是说,24 小时的Period 在夏令时边界上可能少于一天或多于一天。

因此,需要几毫秒的构造函数仅填充精确的字段。要初始化不精确的字段(不考虑夏令时),您需要:

Period twoWeeks = new Period(twentyDaysInMillis).normalizedStandard(PeriodType.weeks()); 

另请参阅:

That'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:

Period twoWeeks = new Period(twentyDaysInMillis).normalizedStandard(PeriodType.weeks()); 

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文