/** * Converts this duration to a period in seconds assuming that there are the * standard number of milliseconds in a second. * <p> * This method assumes that there are 1000 milliseconds in a second. * All currently supplied chronologies use this definition. * * @return a period representing the number of standard seconds in this period, never null * @throws ArithmeticException if the number of seconds is too large to be represented * @since 1.6 */ public Seconds toStandardSeconds() { long seconds = getStandardSeconds(); return Seconds.seconds(FieldUtils.safeToInt(seconds)); }
/** * Returns a new duration with this length plus that specified multiplied by the scalar. * This instance is immutable and is not altered. * <p> * If the addition is zero, this instance is returned. * * @param durationToAdd the duration to add to this one * @param scalar the amount of times to add, such as -1 to subtract once * @return the new duration instance */ public Duration withDurationAdded(long durationToAdd, int scalar) { if (durationToAdd == 0 || scalar == 0) { return this; } long add = FieldUtils.safeMultiply(durationToAdd, scalar); long duration = FieldUtils.safeAdd(getMillis(), add); return new Duration(duration); }
/** * Returns a new instance with the seconds value negated. * * @return the new period with a negated value * @throws ArithmeticException if the result overflows an int */ public Seconds negated() { return Seconds.seconds(FieldUtils.safeNegate(getValue())); }
/** * Multiply two values to return an int throwing an exception if overflow occurs. * * @param val1 the first value * @param val2 the second value * @return the new total * @throws ArithmeticException if the value is too big or too small */ public static int safeMultiplyToInt(long val1, long val2) { long val = FieldUtils.safeMultiply(val1, val2); return FieldUtils.safeToInt(val); }
public long add(long instant, long value) { long addition = FieldUtils.safeMultiply(value, iUnitMillis); return FieldUtils.safeAdd(instant, addition); }
public int getDifference(long minuendInstant, long subtrahendInstant) { return FieldUtils.safeToInt(FieldUtils.safeSubtract(minuendInstant, subtrahendInstant)); }
/** * Returns a new duration with this length multiplied by the * specified multiplicand. * This instance is immutable and is not altered. * <p> * If the multiplicand is one, this instance is returned. * * @param multiplicand the multiplicand to multiply this one by * @return the new duration instance */ public Duration multipliedBy(long multiplicand) { if (multiplicand == 1) { return this; } return new Duration(FieldUtils.safeMultiply(getMillis(), multiplicand)); }
/** * Gets a copy of this Partial with the specified period added. * <p> * If the addition is zero, then <code>this</code> is returned. * Fields in the period that aren't present in the partial are ignored. * <p> * This method is typically used to add multiple copies of complex * period instances. Adding one field is best achieved using the method * {@link #withFieldAdded(DurationFieldType, int)}. * * @param period the period to add to this one, null means zero * @param scalar the amount of times to add, such as -1 to subtract once * @return a copy of this instance with the period added * @throws ArithmeticException if the new datetime exceeds the capacity */ public Partial withPeriodAdded(ReadablePeriod period, int scalar) { if (period == null || scalar == 0) { return this; } int[] newValues = getValues(); for (int i = 0; i < period.size(); i++) { DurationFieldType fieldType = period.getFieldType(i); int index = indexOf(fieldType); if (index >= 0) { newValues = getField(index).add(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar)); } } return new Partial(this, newValues); }
/** * Converts this duration to a period in minutes assuming that there are the * standard number of milliseconds in a minute. * <p> * This method assumes that there are 60 seconds in a minute and * 1000 milliseconds in a second. * All currently supplied chronologies use this definition. * * @return a period representing the number of standard minutes in this period, never null * @throws ArithmeticException if the number of minutes is too large to be represented * @since 2.0 */ public Minutes toStandardMinutes() { long minutes = getStandardMinutes(); return Minutes.minutes(FieldUtils.safeToInt(minutes)); }
/** * Set the specified amount of remainder units to the specified time instant. * * @param instant the time instant in millis to update. * @param value value of remainder units to set. * @return the updated time instant. * @throws IllegalArgumentException if value is too large or too small. */ public long set(long instant, int value) { FieldUtils.verifyValueBounds(this, value, 0, iDivisor - 1); int divided = getDivided(getWrappedField().get(instant)); return getWrappedField().set(instant, divided * iDivisor + value); }
/** * Returns a new instance with the seconds 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 Seconds multipliedBy(int scalar) { return Seconds.seconds(FieldUtils.safeMultiply(getValue(), scalar)); }
for (int i = 0; i < period.size(); i++) { DurationFieldType fieldType = period.getFieldType(i); int index = indexOf(fieldType); if (index >= 0) { newValues = getField(index).addWrapPartial(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar));
/** * Returns a new instance with the minutes value negated. * * @return the new period with a negated value * @throws ArithmeticException if the result overflows an int */ public Minutes negated() { return Minutes.minutes(FieldUtils.safeNegate(getValue())); }
/** * Returns a new instance with the minutes 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 Minutes multipliedBy(int scalar) { return Minutes.minutes(FieldUtils.safeMultiply(getValue(), scalar)); }
/** * Returns a copy of this year-month minus the specified number of years. * <p> * This year-month instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * YearMonth subtracted = ym.minusYears(6); * YearMonth subtracted = ym.minus(Period.years(6)); * YearMonth subtracted = ym.withFieldAdded(DurationFieldType.years(), -6); * </pre> * * @param years the amount of years to subtract, may be negative * @return the new year-month minus the increased years, never null */ public YearMonth minusYears(int years) { return withFieldAdded(DurationFieldType.years(), FieldUtils.safeNegate(years)); }
/** * Returns a new instance with the specified number of seconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param seconds the amount of seconds to add, may be negative * @return the new period plus the specified number of seconds * @throws ArithmeticException if the result overflows an int */ public Seconds plus(int seconds) { if (seconds == 0) { return this; } return Seconds.seconds(FieldUtils.safeAdd(getValue(), seconds)); }
/** * Returns a new instance with the specified number of minutes added. * <p> * This instance is immutable and unaffected by this method call. * * @param minutes the amount of minutes to add, may be negative * @return the new period plus the specified number of minutes * @throws ArithmeticException if the result overflows an int */ public Minutes plus(int minutes) { if (minutes == 0) { return this; } return Minutes.minutes(FieldUtils.safeAdd(getValue(), minutes)); }
/** * Converts this duration to a period in days assuming that there are the * standard number of milliseconds in a day. * <p> * This method assumes that there are 24 hours in a day, * 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in * a second. This will be true for most days, however days with Daylight * Savings changes will not have 24 hours, so use this method with care. * * @return a period representing the number of standard days in this period, never null * @throws ArithmeticException if the number of days is too large to be represented * @since 2.0 */ public Days toStandardDays() { long days = getStandardDays(); return Days.days(FieldUtils.safeToInt(days)); }
/** * Returns a copy of this year-month minus the specified number of months. * <p> * This year-month instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * YearMonth subtracted = ym.minusMonths(6); * YearMonth subtracted = ym.minus(Period.months(6)); * YearMonth subtracted = ym.withFieldAdded(DurationFieldType.months(), -6); * </pre> * * @param months the amount of months to subtract, may be negative * @return the new year-month minus the increased months, never null */ public YearMonth minusMonths(int months) { return withFieldAdded(DurationFieldType.months(), FieldUtils.safeNegate(months)); }
/** * Returns a copy of this date minus the specified number of days. * <p> * This datetime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * YearMonthDay subtracted = dt.minusDays(6); * YearMonthDay subtracted = dt.minus(Period.days(6)); * YearMonthDay subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new datetime minus the increased days * @since 1.1 */ public YearMonthDay minusDays(int days) { return withFieldAdded(DurationFieldType.days(), FieldUtils.safeNegate(days)); }