Well, as has already been stated, finding the containing Duration
interval as you are already doing or using millis directly is sufficient for this use case, and the math involved is straightforward. However, if you did have a use case which warranted a Period
interval involving hours, here's how it could be handled:
- Translate the
Period
into an approximate duration of hours and use that to estimate how many intervals away the target is from the origin.
- Scale the
Period
by your estimate. Treat aggregations of 24 hours as additional days.
- Move the origin interval by the scaled period. If the moved interval contains your target, you're done. If not, re-estimate based on the amount you missed by.
An important thing to note when finding the containing interval is that all Period
addition must take place from the origin directly and not from intermediate intervals for consistency. For example, if you have a Period
of one month with an origin of January 31st, the intervals immediately after the origin interval should begin on February 28th and March 31st. Adding two months to January 31st will correctly yield March 31st, but adding one month to February 28th would incorrectly yield March 28th.
The following is the code for the above approach. Note that there are plenty of anomalous situations for this kind of thing, and I only tested a few of them, so don't consider this code as rigorously tested.
public static final int NUM_HOURS_IN_DAY = 24;
public static final int NUM_HOURS_IN_MONTH = 730; // approximate
public ZonedDateTime startOfContainingInterval(ZonedDateTime origin, Period period, int hours, ZonedDateTime target) {
return intervalStart(origin, period, hours, containingIntervalNum(origin, period, hours, target));
}
public int containingIntervalNum(ZonedDateTime origin, Period period, int hours, ZonedDateTime target) {
int intervalNum = 0;
ZonedDateTime intervalStart = origin, intervalFinish;
long approximatePeriodHours = period.toTotalMonths() * NUM_HOURS_IN_MONTH + period.getDays() * NUM_HOURS_IN_DAY + hours;
do {
long gap = ChronoUnit.HOURS.between(intervalStart, target);
long estimatedIntervalsAway = Math.floorDiv(gap, approximatePeriodHours);
intervalNum += estimatedIntervalsAway;
intervalStart = intervalStart(origin, period, hours, intervalNum);
intervalFinish = intervalStart(origin, period, hours, intervalNum + 1);
} while (!(target.isAfter(intervalStart) && target.isBefore(intervalFinish) || target.equals(intervalStart)));
return intervalNum;
}
public ZonedDateTime intervalStart(ZonedDateTime origin, Period period, int hours, int intervalNum) {
Period scaledPeriod = period.multipliedBy(intervalNum).plusDays(hours * intervalNum / NUM_HOURS_IN_DAY);
long leftoverHours = hours * intervalNum % NUM_HOURS_IN_DAY;
return origin.plus(scaledPeriod).plusHours(leftoverHours);
}
Period
or aDuration
?Period
andDuration
represent different concepts. If your locale observes daylight savings time, four days can be as few as 342,000 seconds or as much as 349,200 seconds.