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

Print this page




 442     //-----------------------------------------------------------------------
 443     /**
 444      * Obtains a {@code Duration} representing the duration between two instants.
 445      * <p>
 446      * This calculates the duration between two temporal objects of the same type.
 447      * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 448      * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 449      * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 450      * <p>
 451      * The result of this method can be a negative period if the end is before the start.
 452      * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 453      *
 454      * @param startInclusive  the start instant, inclusive, not null
 455      * @param endExclusive  the end instant, exclusive, not null
 456      * @return a {@code Duration}, not null
 457      * @throws DateTimeException if the seconds between the temporals cannot be obtained
 458      * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 459      */
 460     public static Duration between(Temporal startInclusive, Temporal endExclusive) {
 461         try {
 462             return ofNanos(startInclusive.periodUntil(endExclusive, NANOS));
 463         } catch (DateTimeException | ArithmeticException ex) {
 464             long secs = startInclusive.periodUntil(endExclusive, SECONDS);
 465             long nanos;
 466             try {
 467                 nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
 468                 if (secs > 0 && nanos < 0) {
 469                     secs++;
 470                 } else if (secs < 0 && nanos > 0) {
 471                     secs--;
 472                 }
 473             } catch (DateTimeException ex2) {
 474                 nanos = 0;
 475             }
 476             return ofSeconds(secs, nanos);
 477         }
 478     }
 479 
 480     //-----------------------------------------------------------------------
 481     /**
 482      * Obtains an instance of {@code Duration} using seconds and nanoseconds.
 483      *
 484      * @param seconds  the length of the duration in seconds, positive or negative


 506     //-----------------------------------------------------------------------
 507     /**
 508      * Gets the value of the requested unit.
 509      * <p>
 510      * This returns a value for each of the two supported units,
 511      * {@link ChronoUnit#SECONDS SECONDS} and {@link ChronoUnit#NANOS NANOS}.
 512      * All other units throw an exception.
 513      *
 514      * @param unit the {@code TemporalUnit} for which to return the value
 515      * @return the long value of the unit
 516      * @throws DateTimeException if the unit is not supported
 517      * @throws UnsupportedTemporalTypeException if the unit is not supported
 518      */
 519     @Override
 520     public long get(TemporalUnit unit) {
 521         if (unit == SECONDS) {
 522             return seconds;
 523         } else if (unit == NANOS) {
 524             return nanos;
 525         } else {
 526             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName());
 527         }
 528     }
 529 
 530     /**
 531      * Gets the set of units supported by this duration.
 532      * <p>
 533      * The supported units are {@link ChronoUnit#SECONDS SECONDS},
 534      * and {@link ChronoUnit#NANOS NANOS}.
 535      * They are returned in the order seconds, nanos.
 536      * <p>
 537      * This set can be used in conjunction with {@link #get(TemporalUnit)}
 538      * to access the entire state of the period.
 539      *
 540      * @return a list containing the seconds and nanos units, not null
 541      */
 542     @Override
 543     public List<TemporalUnit> getUnits() {
 544         return DurationUnits.UNITS;
 545     }
 546 




 442     //-----------------------------------------------------------------------
 443     /**
 444      * Obtains a {@code Duration} representing the duration between two instants.
 445      * <p>
 446      * This calculates the duration between two temporal objects of the same type.
 447      * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 448      * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 449      * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 450      * <p>
 451      * The result of this method can be a negative period if the end is before the start.
 452      * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 453      *
 454      * @param startInclusive  the start instant, inclusive, not null
 455      * @param endExclusive  the end instant, exclusive, not null
 456      * @return a {@code Duration}, not null
 457      * @throws DateTimeException if the seconds between the temporals cannot be obtained
 458      * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 459      */
 460     public static Duration between(Temporal startInclusive, Temporal endExclusive) {
 461         try {
 462             return ofNanos(startInclusive.until(endExclusive, NANOS));
 463         } catch (DateTimeException | ArithmeticException ex) {
 464             long secs = startInclusive.until(endExclusive, SECONDS);
 465             long nanos;
 466             try {
 467                 nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
 468                 if (secs > 0 && nanos < 0) {
 469                     secs++;
 470                 } else if (secs < 0 && nanos > 0) {
 471                     secs--;
 472                 }
 473             } catch (DateTimeException ex2) {
 474                 nanos = 0;
 475             }
 476             return ofSeconds(secs, nanos);
 477         }
 478     }
 479 
 480     //-----------------------------------------------------------------------
 481     /**
 482      * Obtains an instance of {@code Duration} using seconds and nanoseconds.
 483      *
 484      * @param seconds  the length of the duration in seconds, positive or negative


 506     //-----------------------------------------------------------------------
 507     /**
 508      * Gets the value of the requested unit.
 509      * <p>
 510      * This returns a value for each of the two supported units,
 511      * {@link ChronoUnit#SECONDS SECONDS} and {@link ChronoUnit#NANOS NANOS}.
 512      * All other units throw an exception.
 513      *
 514      * @param unit the {@code TemporalUnit} for which to return the value
 515      * @return the long value of the unit
 516      * @throws DateTimeException if the unit is not supported
 517      * @throws UnsupportedTemporalTypeException if the unit is not supported
 518      */
 519     @Override
 520     public long get(TemporalUnit unit) {
 521         if (unit == SECONDS) {
 522             return seconds;
 523         } else if (unit == NANOS) {
 524             return nanos;
 525         } else {
 526             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
 527         }
 528     }
 529 
 530     /**
 531      * Gets the set of units supported by this duration.
 532      * <p>
 533      * The supported units are {@link ChronoUnit#SECONDS SECONDS},
 534      * and {@link ChronoUnit#NANOS NANOS}.
 535      * They are returned in the order seconds, nanos.
 536      * <p>
 537      * This set can be used in conjunction with {@link #get(TemporalUnit)}
 538      * to access the entire state of the period.
 539      *
 540      * @return a list containing the seconds and nanos units, not null
 541      */
 542     @Override
 543     public List<TemporalUnit> getUnits() {
 544         return DurationUnits.UNITS;
 545     }
 546