< prev index next >

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

Print this page




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      */
1374     public Duration truncatedTo(TemporalUnit unit) {
1375         Objects.requireNonNull(unit, "unit");
1376         if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
1377             return new Duration(seconds, 0);
1378         } else if (unit == ChronoUnit.NANOS) {
1379             return this;
1380         }
1381         Duration unitDur = unit.getDuration();
1382         if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
1383             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
1384         }
1385         long dur = unitDur.toNanos();
1386         if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
1387             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
1388         }
1389         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
1390         long result = (nod / dur) * dur ;
1391         return plusNanos(result - nod);
1392     }




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     }


< prev index next >