< prev index next >

src/java.base/share/classes/java/time/Duration.java

Print this page

        

*** 59,68 **** --- 59,69 ---- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package java.time; + import static java.time.LocalTime.MINUTES_PER_HOUR; import static java.time.LocalTime.NANOS_PER_SECOND; import static java.time.LocalTime.SECONDS_PER_DAY; import static java.time.LocalTime.SECONDS_PER_HOUR; import static java.time.LocalTime.SECONDS_PER_MINUTE; import static java.time.temporal.ChronoField.NANO_OF_SECOND;
*** 971,981 **** 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> --- 972,982 ---- return ZERO; } if (multiplicand == 1) { return this; } ! return create(toBigDecimalSeconds().multiply(BigDecimal.valueOf(multiplicand))); } /** * Returns a copy of this duration divided by the specified value. * <p>
*** 990,1009 **** throw new ArithmeticException("Cannot divide by zero"); } if (divisor == 1) { return this; } ! return create(toSeconds().divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN)); } /** * Converts this duration to the total length in seconds and * fractional nanoseconds expressed as a {@code BigDecimal}. * * @return the total length of the duration in seconds, with a scale of 9, not null */ ! private BigDecimal toSeconds() { return BigDecimal.valueOf(seconds).add(BigDecimal.valueOf(nanos, 9)); } /** * Creates an instance of {@code Duration} from a number of seconds. --- 991,1010 ---- throw new ArithmeticException("Cannot divide by zero"); } if (divisor == 1) { return this; } ! return create(toBigDecimalSeconds().divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN)); } /** * Converts this duration to the total length in seconds and * fractional nanoseconds expressed as a {@code BigDecimal}. * * @return the total length of the duration in seconds, with a scale of 9, not null */ ! private BigDecimal toBigDecimalSeconds() { return BigDecimal.valueOf(seconds).add(BigDecimal.valueOf(nanos, 9)); } /** * Creates an instance of {@code Duration} from a number of seconds.
*** 1166,1175 **** --- 1167,1190 ---- public long toMinutes() { return seconds / SECONDS_PER_MINUTE; } /** + * Gets the number of seconds in this duration. + * <p> + * This returns the total number of seconds in the duration + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of seconds in the duration, may be negative + * @since 9 + */ + public long toSeconds() { + return seconds; + } + + /** * Converts this duration to the total length in milliseconds. * <p> * If this duration is too large to fit in a {@code long} milliseconds, then an * exception is thrown. * <p>
*** 1199,1208 **** --- 1214,1323 ---- long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND); totalNanos = Math.addExact(totalNanos, nanos); return totalNanos; } + /** + * Extracts the number of days in the duration. + * <p> + * This returns the total number of days in the duration by dividing the + * number of seconds by 86400. + * This is based on the standard definition of a day as 24 hours. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of days in the duration, may be negative + * @since 9 + */ + public long toDaysPart(){ + return seconds / SECONDS_PER_DAY; + } + + /** + * Extracts the number of hours part in the duration. + * <p> + * This returns the number of remaining hours when dividing {@link #toHours} + * by hours in a day. + * This is based on the standard definition of a day as 24 hours. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of hours part in the duration, may be negative + * @since 9 + */ + public int toHoursPart(){ + return (int) (toHours() % 24); + } + + /** + * Extracts the number of minutes part in the duration. + * <p> + * This returns the number of remaining minutes when dividing {@link #toMinutes} + * by minutes in an hour. + * This is based on the standard definition of an hour as 60 minutes. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of minutes parts in the duration, may be negative + * @since 9 + * may be negative + */ + public int toMinutesPart(){ + return (int) (toMinutes() % MINUTES_PER_HOUR); + } + + /** + * Extracts the number of seconds part in the duration. + * <p> + * This returns the remaining seconds when dividing {@link #toSeconds} + * by seconds in a minute. + * This is based on the standard definition of a minute as 60 seconds. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of seconds parts in the duration, may be negative + * @since 9 + */ + public int toSecondsPart(){ + return (int) (seconds % SECONDS_PER_MINUTE); + } + + /** + * Extracts the number of milliseconds part of the duration. + * <p> + * This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000. + * The length of the duration is stored using two fields - seconds and nanoseconds. + * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to + * the length in seconds. + * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the number of milliseconds part of the duration. + * @since 9 + */ + public int toMillisPart(){ + return nanos / 1000_000; + } + + /** + * Get the nanoseconds part within seconds of the duration. + * <p> + * The length of the duration is stored using two fields - seconds and nanoseconds. + * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to + * the length in seconds. + * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999 + * @since 9 + */ + public int toNanosPart(){ + return nanos; + } + //----------------------------------------------------------------------- /** * Compares this duration to the specified {@code Duration}. * <p> * The comparison is based on the total length of the durations.
< prev index next >