/** * Obtains an instance of {@code Duration} from a number of seconds. * <p> * The nanosecond in second field is set to zero. * * @param seconds the number of seconds, positive or negative * @return a {@code Duration}, not null */ public static Duration ofSeconds(long seconds) { return create(seconds, 0); }
/** * Returns a copy of this duration with the specified amount of seconds. * <p> * This returns a duration with the specified seconds, retaining the * nano-of-second part of this duration. * <p> * This instance is immutable and unaffected by this method call. * * @param seconds the seconds to represent, may be negative * @return a {@code Duration} based on this period with the requested seconds, not null */ public Duration withSeconds(long seconds) { return create(seconds, nanos); }
/** * Obtains an instance of {@code Duration} from a number of nanoseconds. * <p> * The seconds and nanoseconds are extracted from the specified nanoseconds. * * @param nanos the number of nanoseconds, positive or negative * @return a {@code Duration}, not null */ public static Duration ofNanos(long nanos) { long secs = nanos / NANOS_PER_SECOND; int nos = (int) (nanos % NANOS_PER_SECOND); if (nos < 0) { nos += NANOS_PER_SECOND; secs--; } return create(secs, nos); }
/** * Obtains an instance of {@code Duration} from a number of milliseconds. * <p> * The seconds and nanoseconds are extracted from the specified milliseconds. * * @param millis the number of milliseconds, positive or negative * @return a {@code Duration}, not null */ public static Duration ofMillis(long millis) { long secs = millis / 1000; int mos = (int) (millis % 1000); if (mos < 0) { mos += 1000; secs--; } return create(secs, mos * NANOS_PER_MILLI); }
/** * Obtains an instance of {@code Duration} from a number of standard length minutes. * <p> * The seconds are calculated based on the standard definition of a minute, * where each minute is 60 seconds. * The nanosecond in second field is set to zero. * * @param minutes the number of minutes, positive or negative * @return a {@code Duration}, not null * @throws ArithmeticException if the input minutes exceeds the capacity of {@code Duration} */ public static Duration ofMinutes(long minutes) { return create(Jdk8Methods.safeMultiply(minutes, 60), 0); }
/** * Obtains an instance of {@code Duration} from a number of standard 24 hour days. * <p> * The seconds are calculated based on the standard definition of a day, * where each day is 86400 seconds which implies a 24 hour day. * The nanosecond in second field is set to zero. * * @param days the number of days, positive or negative * @return a {@code Duration}, not null * @throws ArithmeticException if the input days exceeds the capacity of {@code Duration} */ public static Duration ofDays(long days) { return create(Jdk8Methods.safeMultiply(days, 86400), 0); }
/** * Obtains an instance of {@code Duration} from a number of standard length hours. * <p> * The seconds are calculated based on the standard definition of an hour, * where each hour is 3600 seconds. * The nanosecond in second field is set to zero. * * @param hours the number of hours, positive or negative * @return a {@code Duration}, not null * @throws ArithmeticException if the input hours exceeds the capacity of {@code Duration} */ public static Duration ofHours(long hours) { return create(Jdk8Methods.safeMultiply(hours, 3600), 0); }
/** * Returns a copy of this duration with the specified nano-of-second. * <p> * This returns a duration with the specified nano-of-second, retaining the * seconds part of this duration. * <p> * This instance is immutable and unaffected by this method call. * * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 * @return a {@code Duration} based on this period with the requested nano-of-second, not null * @throws DateTimeException if the nano-of-second is invalid */ public Duration withNanos(int nanoOfSecond) { NANO_OF_SECOND.checkValidIntValue(nanoOfSecond); return create(seconds, nanoOfSecond); }
/** * Returns a copy of this duration multiplied by the scalar. * <p> * This instance is immutable and unaffected by this method call. * * @param multiplicand the value to multiply the duration by, positive or negative * @return a {@code Duration} based on this duration multiplied by the specified scalar, not null * @throws ArithmeticException if numeric overflow occurs */ public Duration multipliedBy(long multiplicand) { if (multiplicand == 0) { return ZERO; } if (multiplicand == 1) { return this; } return create(toSeconds().multiply(BigDecimal.valueOf(multiplicand))); }
/** * Returns a copy of this duration divided by the specified value. * <p> * This instance is immutable and unaffected by this method call. * * @param divisor the value to divide the duration by, positive or negative, not zero * @return a {@code Duration} based on this duration divided by the specified divisor, not null * @throws ArithmeticException if the divisor is zero * @throws ArithmeticException if numeric overflow occurs */ public Duration dividedBy(long divisor) { if (divisor == 0) { throw new ArithmeticException("Cannot divide by zero"); } if (divisor == 1) { return this; } return create(toSeconds().divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN)); }
/** * Obtains an instance of {@code Duration} from a number of seconds * and an adjustment in nanoseconds. * <p> * This method allows an arbitrary number of nanoseconds to be passed in. * The factory will alter the values of the second and nanosecond in order * to ensure that the stored nanosecond is in the range 0 to 999,999,999. * For example, the following will result in the exactly the same duration: * <pre> * Duration.ofSeconds(3, 1); * Duration.ofSeconds(4, -999_999_999); * Duration.ofSeconds(2, 1000_000_001); * </pre> * * @param seconds the number of seconds, positive or negative * @param nanoAdjustment the nanosecond adjustment to the number of seconds, positive or negative * @return a {@code Duration}, not null * @throws ArithmeticException if the adjustment causes the seconds to exceed the capacity of {@code Duration} */ public static Duration ofSeconds(long seconds, long nanoAdjustment) { long secs = Jdk8Methods.safeAdd(seconds, Jdk8Methods.floorDiv(nanoAdjustment, NANOS_PER_SECOND)); int nos = Jdk8Methods.floorMod(nanoAdjustment, NANOS_PER_SECOND); return create(secs, nos); }
int nanos = parseFraction(text, fractionMatch, seconds < 0 ? -1 : 1); try { return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos); } catch (ArithmeticException ex) { throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);