LocalTime now = LocalTime.now(); LocalTime later = now.plus(2, HOURS);
class Flight { LocalTime departure; Duration duration; LocalTime getArrival() { LocalTime arrival = departure.plus( duration ); // If called *many* times, and you account for changing-data and thread-safety, you could cache this result for performance. return arrival; } Flight( LocalTime departureArg , Duration durationArg ) { this.departure = departureArg; this.duration = durationArg; } }
/** * Returns a copy of this time with the specified period subtracted. * <p> * This method returns a new time based on this time with the specified period subtracted. * This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds. * The unit is responsible for the details of the calculation, including the resolution * of any edge cases in the calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToSubtract the amount of the unit to subtract from the result, may be negative * @param unit the unit of the period to subtract, not null * @return a {@code LocalTime} based on this time with the specified period subtracted, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalTime minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); }
/** * Returns a copy of this time with the specified period subtracted. * <p> * This method returns a new time based on this time with the specified period subtracted. * This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds. * The unit is responsible for the details of the calculation, including the resolution * of any edge cases in the calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToSubtract the amount of the unit to subtract from the result, may be negative * @param unit the unit of the period to subtract, not null * @return a {@code LocalTime} based on this time with the specified period subtracted, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalTime minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); }
LocalTime lt = LocalTime.parse("10:00:00", DateTimeFormat.forPattern("HH:mm:ss")); lt = lt.plus(p); System.out.println(lt);
DateMidnight midnight = new DateTime("2012-12-13T21:39:45.618").toDateMidnight(); LocalTime localTime = new LocalTime(midnight); localTime.plus(period); formatter.print(localTime);
/** * Returns a copy of this time with the specified period added. * <p> * This method returns a new time based on this time with the specified period added. * This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds. * The unit is responsible for the details of the calculation, including the resolution * of any edge cases in the calculation. * The offset is not part of the calculation and will be unchanged in the result. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the period to add, not null * @return an {@code OffsetTime} based on this time with the specified period added, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public OffsetTime plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { return with(time.plus(amountToAdd, unit), offset); } return unit.addTo(this, amountToAdd); }
/** * Returns a copy of this time with the specified period added. * <p> * This method returns a new time based on this time with the specified period added. * This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds. * The unit is responsible for the details of the calculation, including the resolution * of any edge cases in the calculation. * The offset is not part of the calculation and will be unchanged in the result. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the period to add, not null * @return an {@code OffsetTime} based on this time with the specified period added, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public OffsetTime plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { return with(time.plus(amountToAdd, unit), offset); } return unit.addTo(this, amountToAdd); }