开发者

Bug in jodatime Period?

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 P开发者_如何学JAVAeriodTypes 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.


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:

  • Period javadoc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜