1333 return nanos / 1000_000; 1334 } 1335 1336 /** 1337 * Get the nanoseconds part within seconds of the duration. 1338 * <p> 1339 * The length of the duration is stored using two fields - seconds and nanoseconds. 1340 * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to 1341 * the length in seconds. 1342 * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}. 1343 * <p> 1344 * This instance is immutable and unaffected by this method call. 1345 * 1346 * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999 1347 * @since 9 1348 */ 1349 public int toNanosPart(){ 1350 return nanos; 1351 } 1352 1353 //----------------------------------------------------------------------- 1354 /** 1355 * Compares this duration to the specified {@code Duration}. 1356 * <p> 1357 * The comparison is based on the total length of the durations. 1358 * It is "consistent with equals", as defined by {@link Comparable}. 1359 * 1360 * @param otherDuration the other duration to compare to, not null 1361 * @return the comparator value, negative if less, positive if greater 1362 */ 1363 @Override 1364 public int compareTo(Duration otherDuration) { 1365 int cmp = Long.compare(seconds, otherDuration.seconds); 1366 if (cmp != 0) { 1367 return cmp; 1368 } 1369 return nanos - otherDuration.nanos; 1370 } 1371 1372 //----------------------------------------------------------------------- | 1333 return nanos / 1000_000; 1334 } 1335 1336 /** 1337 * Get the nanoseconds part within seconds of the duration. 1338 * <p> 1339 * The length of the duration is stored using two fields - seconds and nanoseconds. 1340 * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to 1341 * the length in seconds. 1342 * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}. 1343 * <p> 1344 * This instance is immutable and unaffected by this method call. 1345 * 1346 * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999 1347 * @since 9 1348 */ 1349 public int toNanosPart(){ 1350 return nanos; 1351 } 1352 1353 1354 //----------------------------------------------------------------------- 1355 /** 1356 * Returns a copy of this {@code Duration} truncated to the specified unit. 1357 * <p> 1358 * Truncating the duration returns a copy of the original with fields 1359 * smaller than the specified unit set to zero. 1360 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will 1361 * round down to the nearest minute, setting the seconds and 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 supplied time units on {@link ChronoUnit} and 1366 * {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception. 1367 * <p> 1368 * This instance is immutable and unaffected by this method call. 1369 * 1370 * @param unit the unit to truncate to, not null 1371 * @return a {@code Duration} based on this duration with the time truncated, not null 1372 * @throws DateTimeException if the unit is invalid for truncation 1373 * @throws UnsupportedTemporalTypeException if the unit is not supported 1374 */ 1375 public Duration truncatedTo(TemporalUnit unit) { 1376 Objects.requireNonNull(unit, "unit"); 1377 if (unit == ChronoUnit.SECONDS) { 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 = Math.floorDiv(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; 1412 } 1413 1414 //----------------------------------------------------------------------- |