< prev index next >

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

Print this page
8204444: java.time cleanup
Reviewed-by: scolebourne, rriggs


 214     //-----------------------------------------------------------------------
 215     /**
 216      * Obtains a {@code Duration} representing a number of seconds.
 217      * <p>
 218      * The nanosecond in second field is set to zero.
 219      *
 220      * @param seconds  the number of seconds, positive or negative
 221      * @return a {@code Duration}, not null
 222      */
 223     public static Duration ofSeconds(long seconds) {
 224         return create(seconds, 0);
 225     }
 226 
 227     /**
 228      * Obtains a {@code Duration} representing a number of seconds and an
 229      * adjustment in nanoseconds.
 230      * <p>
 231      * This method allows an arbitrary number of nanoseconds to be passed in.
 232      * The factory will alter the values of the second and nanosecond in order
 233      * to ensure that the stored nanosecond is in the range 0 to 999,999,999.
 234      * For example, the following will result in the exactly the same duration:
 235      * <pre>
 236      *  Duration.ofSeconds(3, 1);
 237      *  Duration.ofSeconds(4, -999_999_999);
 238      *  Duration.ofSeconds(2, 1000_000_001);
 239      * </pre>
 240      *
 241      * @param seconds  the number of seconds, positive or negative
 242      * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
 243      * @return a {@code Duration}, not null
 244      * @throws ArithmeticException if the adjustment causes the seconds to exceed the capacity of {@code Duration}
 245      */
 246     public static Duration ofSeconds(long seconds, long nanoAdjustment) {
 247         long secs = Math.addExact(seconds, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
 248         int nos = (int) Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
 249         return create(secs, nos);
 250     }
 251 
 252     //-----------------------------------------------------------------------
 253     /**
 254      * Obtains a {@code Duration} representing a number of milliseconds.


1340      * the length in seconds.
1341      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1342      * <p>
1343      * This instance is immutable and unaffected by this method call.
1344      *
1345      * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1346      * @since 9
1347      */
1348     public int toNanosPart(){
1349         return nanos;
1350     }
1351 
1352 
1353     //-----------------------------------------------------------------------
1354     /**
1355      * Returns a copy of this {@code Duration} truncated to the specified unit.
1356      * <p>
1357      * Truncating the duration returns a copy of the original with conceptual fields
1358      * smaller than the specified unit set to zero.
1359      * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
1360      * round down to the nearest minute, setting the seconds and nanoseconds to zero.

1361      * <p>
1362      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
1363      * that divides into the length of a standard day without remainder.
1364      * This includes all supplied time units on {@link ChronoUnit} and
1365      * {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.

1366      * <p>
1367      * This instance is immutable and unaffected by this method call.
1368      *
1369      * @param unit the unit to truncate to, not null
1370      * @return a {@code Duration} based on this duration with the time truncated, not null
1371      * @throws DateTimeException if the unit is invalid for truncation
1372      * @throws UnsupportedTemporalTypeException if the unit is not supported
1373      * @since 9
1374      */
1375     public Duration truncatedTo(TemporalUnit unit) {
1376         Objects.requireNonNull(unit, "unit");
1377         if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
1378             return new Duration(seconds, 0);
1379         } else if (unit == ChronoUnit.NANOS) {
1380             return this;
1381         }
1382         Duration unitDur = unit.getDuration();
1383         if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
1384             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
1385         }
1386         long dur = unitDur.toNanos();
1387         if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
1388             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
1389         }
1390         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
1391         long result = (nod / dur) * dur ;
1392         return plusNanos(result - nod);
1393     }
1394 
1395     //-----------------------------------------------------------------------
1396     /**
1397      * Compares this duration to the specified {@code Duration}.
1398      * <p>
1399      * The comparison is based on the total length of the durations.
1400      * It is "consistent with equals", as defined by {@link Comparable}.
1401      *
1402      * @param otherDuration the other duration to compare to, not null
1403      * @return the comparator value, negative if less, positive if greater
1404      */
1405     @Override
1406     public int compareTo(Duration otherDuration) {
1407         int cmp = Long.compare(seconds, otherDuration.seconds);
1408         if (cmp != 0) {
1409             return cmp;
1410         }
1411         return nanos - otherDuration.nanos;




 214     //-----------------------------------------------------------------------
 215     /**
 216      * Obtains a {@code Duration} representing a number of seconds.
 217      * <p>
 218      * The nanosecond in second field is set to zero.
 219      *
 220      * @param seconds  the number of seconds, positive or negative
 221      * @return a {@code Duration}, not null
 222      */
 223     public static Duration ofSeconds(long seconds) {
 224         return create(seconds, 0);
 225     }
 226 
 227     /**
 228      * Obtains a {@code Duration} representing a number of seconds and an
 229      * adjustment in nanoseconds.
 230      * <p>
 231      * This method allows an arbitrary number of nanoseconds to be passed in.
 232      * The factory will alter the values of the second and nanosecond in order
 233      * to ensure that the stored nanosecond is in the range 0 to 999,999,999.
 234      * For example, the following will result in exactly the same duration:
 235      * <pre>
 236      *  Duration.ofSeconds(3, 1);
 237      *  Duration.ofSeconds(4, -999_999_999);
 238      *  Duration.ofSeconds(2, 1000_000_001);
 239      * </pre>
 240      *
 241      * @param seconds  the number of seconds, positive or negative
 242      * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
 243      * @return a {@code Duration}, not null
 244      * @throws ArithmeticException if the adjustment causes the seconds to exceed the capacity of {@code Duration}
 245      */
 246     public static Duration ofSeconds(long seconds, long nanoAdjustment) {
 247         long secs = Math.addExact(seconds, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
 248         int nos = (int) Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
 249         return create(secs, nos);
 250     }
 251 
 252     //-----------------------------------------------------------------------
 253     /**
 254      * Obtains a {@code Duration} representing a number of milliseconds.


1340      * the length in seconds.
1341      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1342      * <p>
1343      * This instance is immutable and unaffected by this method call.
1344      *
1345      * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1346      * @since 9
1347      */
1348     public int toNanosPart(){
1349         return nanos;
1350     }
1351 
1352 
1353     //-----------------------------------------------------------------------
1354     /**
1355      * Returns a copy of this {@code Duration} truncated to the specified unit.
1356      * <p>
1357      * Truncating the duration returns a copy of the original with conceptual fields
1358      * smaller than the specified unit set to zero.
1359      * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
1360      * round down towards zero to the nearest minute, setting the seconds and
1361      * nanoseconds to zero.
1362      * <p>
1363      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
1364      * that divides into the length of a standard day without remainder.
1365      * This includes all
1366      * {@linkplain ChronoUnit#isTimeBased() time-based units on {@code ChronoUnit}}
1367      * and {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.
1368      * <p>
1369      * This instance is immutable and unaffected by this method call.
1370      *
1371      * @param unit the unit to truncate to, not null
1372      * @return a {@code Duration} based on this duration with the time truncated, not null
1373      * @throws DateTimeException if the unit is invalid for truncation
1374      * @throws UnsupportedTemporalTypeException if the unit is not supported
1375      * @since 9
1376      */
1377     public Duration truncatedTo(TemporalUnit unit) {
1378         Objects.requireNonNull(unit, "unit");
1379         if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
1380             return new Duration(seconds, 0);
1381         } else if (unit == ChronoUnit.NANOS) {
1382             return this;
1383         }
1384         Duration unitDur = unit.getDuration();
1385         if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
1386             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
1387         }
1388         long dur = unitDur.toNanos();
1389         if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
1390             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
1391         }
1392         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
1393         long result = (nod / dur) * dur;
1394         return plusNanos(result - nod);
1395     }
1396 
1397     //-----------------------------------------------------------------------
1398     /**
1399      * Compares this duration to the specified {@code Duration}.
1400      * <p>
1401      * The comparison is based on the total length of the durations.
1402      * It is "consistent with equals", as defined by {@link Comparable}.
1403      *
1404      * @param otherDuration the other duration to compare to, not null
1405      * @return the comparator value, negative if less, positive if greater
1406      */
1407     @Override
1408     public int compareTo(Duration otherDuration) {
1409         int cmp = Long.compare(seconds, otherDuration.seconds);
1410         if (cmp != 0) {
1411             return cmp;
1412         }
1413         return nanos - otherDuration.nanos;


< prev index next >