Reputation: 86747
Can someone explain why the following tests fails?
@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
}
Upvotes: 2
Views: 2707
Reputation: 10241
A Period
in JodaTime represents a "set of duration field values". Therefore, the methods getMinutes()
, getHours()
, ... are returning the value of those fields rather than computing the minutes, hours, ...
Furthermore, the conversion from a Duration
sets the fields according to a PeriodType
and a chronology.
From the 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.
This means, as a Duration
's day has exactly 24 hours, all information is stored in the hours field and minutes stays at 0.
See this question for a good explanation of the differences between Interval
, Duration
and Period
.
Upvotes: 3
Reputation:
See 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).
Upvotes: 1