Exemplo n.º 1
0
 /**
  * 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);
 }
Exemplo n.º 2
0
 /**
  * Creates a new <code>Hours</code> by parsing a string in the ISO8601 format 'PTnH'.
  *
  * <p>The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the hours
  * component may be non-zero. If any other component is non-zero, an exception will be thrown.
  *
  * @param periodStr the period string, null returns zero
  * @return the period in hours
  * @throws IllegalArgumentException if the string format is invalid
  */
 public static Hours parseHours(String periodStr) {
   if (periodStr == null) {
     return Hours.ZERO;
   }
   Period p = PARSER.parsePeriod(periodStr);
   return Hours.hours(p.getHours());
 }
Exemplo n.º 3
0
 /**
  * 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);
 }
Exemplo n.º 4
0
 /**
  * Is this hours instance less than the specified number of hours.
  *
  * @param other the other period, null means zero
  * @return true if this hours instance is less than the specified one
  */
 public boolean isLessThan(Hours other) {
   if (other == null) {
     return getValue() < 0;
   }
   return getValue() < other.getValue();
 }
Exemplo n.º 5
0
 /**
  * Is this hours instance greater than the specified number of hours.
  *
  * @param other the other period, null means zero
  * @return true if this hours instance is greater than the specified one
  */
 public boolean isGreaterThan(Hours other) {
   if (other == null) {
     return getValue() > 0;
   }
   return getValue() > other.getValue();
 }
Exemplo n.º 6
0
 /**
  * Returns a new instance with the hours value negated.
  *
  * @return the new period with a negated value
  * @throws ArithmeticException if the result overflows an int
  */
 public Hours negated() {
   return Hours.hours(FieldUtils.safeNegate(getValue()));
 }
Exemplo n.º 7
0
 /**
  * Returns a new instance with the hours divided by the specified divisor. The calculation uses
  * integer division, thus 3 divided by 2 is 1.
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param divisor the amount to divide by, may be negative
  * @return the new period divided by the specified divisor
  * @throws ArithmeticException if the divisor is zero
  */
 public Hours dividedBy(int divisor) {
   if (divisor == 1) {
     return this;
   }
   return Hours.hours(getValue() / divisor);
 }
Exemplo n.º 8
0
 /**
  * Returns a new instance with the hours multiplied by the specified scalar.
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param scalar the amount to multiply by, may be negative
  * @return the new period multiplied by the specified scalar
  * @throws ArithmeticException if the result overflows an int
  */
 public Hours multipliedBy(int scalar) {
   return Hours.hours(FieldUtils.safeMultiply(getValue(), scalar));
 }
Exemplo n.º 9
0
 /**
  * Returns a new instance with the specified number of hours taken away.
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param hours the amount of hours to take away, may be negative, null means zero
  * @return the new period minus the specified number of hours
  * @throws ArithmeticException if the result overflows an int
  */
 public Hours minus(Hours hours) {
   if (hours == null) {
     return this;
   }
   return minus(hours.getValue());
 }
Exemplo n.º 10
0
 /**
  * Returns a new instance with the specified number of hours added.
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param hours the amount of hours to add, may be negative
  * @return the new period plus the specified number of hours
  * @throws ArithmeticException if the result overflows an int
  */
 public Hours plus(int hours) {
   if (hours == 0) {
     return this;
   }
   return Hours.hours(FieldUtils.safeAdd(getValue(), hours));
 }
Exemplo n.º 11
0
 /**
  * Resolves singletons.
  *
  * @return the singleton instance
  */
 private Object readResolve() {
   return Hours.hours(getValue());
 }
Exemplo n.º 12
0
 /**
  * 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);
 }
Exemplo n.º 13
0
 /**
  * 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);
 }