private long truncateMillisPeriod(final long t) { // toStandardDuration assumes days are always 24h, and hours are always 60 minutes, // which may not always be the case, e.g if there are daylight saving changes. if (chronology.days().isPrecise() && chronology.hours().isPrecise()) { final long millis = period.toStandardDuration().getMillis(); long offset = t % millis - origin % millis; if (offset < 0) { offset += millis; } return t - offset; } else { throw new UnsupportedOperationException( "Period cannot be converted to milliseconds as some fields mays vary in length with chronology " + chronology ); } }
/** * Returns a copy of this datetime plus the specified number of hours. * <p> * The calculation will add a duration equivalent to the number of hours * expressed in milliseconds. * <p> * For example, if a spring daylight savings cutover is from 01:59 to 03:00 * then adding one hour to 01:30 will result in 03:30. This is a duration * of one hour later, even though the hour field value changed from 1 to 3. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusHours(6); * DateTime added = dt.plus(Period.hours(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param hours the amount of hours to add, may be negative * @return the new datetime plus the increased hours * @since 1.1 */ public DateTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getMillis(), hours); return withMillis(instant); }
return this; long instant = getChronology().hours().subtract(getMillis(), hours); return withMillis(instant);
/** * Add a number of hours to the date. * * @param hours the hours to add * @throws IllegalArgumentException if the value is invalid */ public void addHours(final int hours) { if (hours != 0) { setMillis(getChronology().hours().add(getMillis(), hours)); } }
/** * Returns a copy of this datetime plus the specified number of hours. * <p> * The calculation will add a duration equivalent to the number of hours * expressed in milliseconds. * <p> * For example, if a spring daylight savings cutover is from 01:59 to 03:00 * then adding one hour to 01:30 will result in 03:30. This is a duration * of one hour later, even though the hour field value changed from 1 to 3. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusHours(6); * DateTime added = dt.plus(Period.hours(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param hours the amount of hours to add, may be negative * @return the new datetime plus the increased hours * @since 1.1 */ public DateTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getMillis(), hours); return withMillis(instant); }
return this; long instant = getChronology().hours().subtract(getMillis(), hours); return withMillis(instant);
/** * Add a number of hours to the date. * * @param hours the hours to add * @throws IllegalArgumentException if the value is invalid */ public void addHours(final int hours) { if (hours != 0) { setMillis(getChronology().hours().add(getMillis(), hours)); } }
private long truncateMillisPeriod(final long t) { // toStandardDuration assumes days are always 24h, and hours are always 60 minutes, // which may not always be the case, e.g if there are daylight saving changes. if (chronology.days().isPrecise() && chronology.hours().isPrecise()) { final long millis = period.toStandardDuration().getMillis(); long offset = t % millis - origin % millis; if (offset < 0) { offset += millis; } return t - offset; } else { throw new UnsupportedOperationException( "Period cannot be converted to milliseconds as some fields mays vary in length with chronology " + chronology.toString()); } }
private long truncateMillisPeriod(final long t) { // toStandardDuration assumes days are always 24h, and hours are always 60 minutes, // which may not always be the case, e.g if there are daylight saving changes. if (chronology.days().isPrecise() && chronology.hours().isPrecise()) { final long millis = period.toStandardDuration().getMillis(); long offset = t % millis - origin % millis; if (offset < 0) { offset += millis; } return t - offset; } else { throw new UnsupportedOperationException( "Period cannot be converted to milliseconds as some fields mays vary in length with chronology " + chronology.toString()); } }
/** * Returns a copy of this datetime minus the specified number of hours. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime subtracted = dt.minusHours(6); * LocalDateTime subtracted = dt.minus(Period.hours(6)); * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); * </pre> * * @param hours the amount of hours to subtract, may be negative * @return the new LocalDateTime minus the increased hours */ public LocalDateTime minusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().subtract(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of hours. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime added = dt.plusHours(6); * LocalDateTime added = dt.plus(Period.hours(6)); * LocalDateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * * @param hours the amount of hours to add, may be negative * @return the new LocalDateTime plus the increased hours */ public LocalDateTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this time minus the specified number of hours. * <p> * This LocalTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalTime subtracted = dt.minusHours(6); * LocalTime subtracted = dt.minus(Period.hours(6)); * LocalTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); * </pre> * * @param hours the amount of hours to subtract, may be negative * @return the new LocalTime minus the increased hours */ public LocalTime minusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().subtract(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this time plus the specified number of hours. * <p> * This LocalTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalTime added = dt.plusHours(6); * LocalTime added = dt.plus(Period.hours(6)); * LocalTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * * @param hours the amount of hours to add, may be negative * @return the new LocalTime plus the increased hours */ public LocalTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this time minus the specified number of hours. * <p> * This LocalTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalTime subtracted = dt.minusHours(6); * LocalTime subtracted = dt.minus(Period.hours(6)); * LocalTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); * </pre> * * @param hours the amount of hours to subtract, may be negative * @return the new LocalTime minus the increased hours */ public LocalTime minusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().subtract(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of hours. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime added = dt.plusHours(6); * LocalDateTime added = dt.plus(Period.hours(6)); * LocalDateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * * @param hours the amount of hours to add, may be negative * @return the new LocalDateTime plus the increased hours */ public LocalDateTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this time plus the specified number of hours. * <p> * This LocalTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalTime added = dt.plusHours(6); * LocalTime added = dt.plus(Period.hours(6)); * LocalTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * * @param hours the amount of hours to add, may be negative * @return the new LocalTime plus the increased hours */ public LocalTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * Returns a copy of this datetime minus the specified number of hours. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime subtracted = dt.minusHours(6); * LocalDateTime subtracted = dt.minus(Period.hours(6)); * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); * </pre> * * @param hours the amount of hours to subtract, may be negative * @return the new LocalDateTime minus the increased hours */ public LocalDateTime minusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().subtract(getLocalMillis(), hours); return withLocalMillis(instant); }
/** * 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 <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); }
return chronology.halfdays(); case HOURS: return chronology.hours(); case MINUTES: return chronology.minutes();