/** * Creates a <code>Hours</code> representing the number of whole hours in the specified interval. * * @param interval the interval to extract hours from, null returns zero * @return the period in hours * @throws IllegalArgumentException if the partials are null or invalid */ public static Hours hoursIn(ReadableInterval interval) { if (interval == null) { return Hours.ZERO; } int amount = BaseSingleFieldPeriod.between( interval.getStart(), interval.getEnd(), DurationFieldType.hours()); return Hours.hours(amount); }
/** * Creates a <code>Hours</code> representing the number of whole hours between the two specified * partial datetimes. * * <p>The two partials must contain the same fields, for example you can specify two <code> * LocalTime</code> objects. * * @param start the start partial date, must not be null * @param end the end partial date, must not be null * @return the period in hours * @throws IllegalArgumentException if the partials are null or invalid */ public static Hours hoursBetween(ReadablePartial start, ReadablePartial end) { if (start instanceof LocalTime && end instanceof LocalTime) { Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); int hours = chrono .hours() .getDifference( ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis()); return Hours.hours(hours); } int amount = BaseSingleFieldPeriod.between(start, end, ZERO); return Hours.hours(amount); }
/** * Creates a new <code>Hours</code> representing the number of complete standard length hours in * the specified period. * * <p>This factory method converts all fields from the period to hours using standardised * durations for each field. Only those fields which have a precise duration in the ISO UTC * chronology can be converted. * * <ul> * <li>One week consists of 7 days. * <li>One day consists of 24 hours. * <li>One hour consists of 60 minutes. * <li>One minute consists of 60 seconds. * <li>One second consists of 1000 milliseconds. * </ul> * * Months and Years are imprecise and periods containing these values cannot be converted. * * @param period the period to get the number of hours from, null returns zero * @return the period in hours * @throws IllegalArgumentException if the period contains imprecise duration values */ public static Hours standardHoursIn(ReadablePeriod period) { int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_HOUR); return Hours.hours(amount); }
/** * Creates a <code>Hours</code> representing the number of whole hours between the two specified * datetimes. * * @param start the start instant, must not be null * @param end the end instant, must not be null * @return the period in hours * @throws IllegalArgumentException if the instants are null or invalid */ public static Hours hoursBetween(ReadableInstant start, ReadableInstant end) { int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.hours()); return Hours.hours(amount); }