/** * Subtracts a specified {@link Duration} from another specified {@link Duration}. * @param left The subtrahend. * @param right The minuend. * @return The difference {@code left} minus {@code right}. */ @CS2JInfo("This method implements the functionality of the overloaded operator: 'Duration -(Duration,Duration)'") @Nonnull public static Duration subtract(@Nonnull Duration left, @Nonnull Duration right) { return left.subtract(right); }
/** * Returns {@code true} if this {@link Duration} is within {@code epsilon} seconds of the specified {@link Duration}. That is, in order for the {@link Duration Durations} to be considered equal (and for this function to return {@code true}), the absolute value of the difference between them, in seconds, must be less than or equal to {@code epsilon}. * @param other The {@link Duration} to compare to this {@link Duration}. * @param epsilon The largest difference between the {@link Duration Durations}, in seconds, such that they will be considered equal. * @return {@code true} if the dates are equal as defined by the epsilon value. */ @CS2JWarning("Unhandled attribute removed: Pure") public final boolean equalsEpsilon(@Nonnull Duration other, double epsilon) { return Math.abs(subtract(other).getTotalSeconds()) <= epsilon; }
Duration first = new Duration(5, 1000.0); Duration second = new Duration(5, 2000.0); Duration difference = Duration.subtract(second, first); Assert.assertEquals(1000.0, difference.getTotalSeconds(), 0d); difference = Duration.subtract(first, second); Assert.assertEquals(-1000.0, difference.getTotalSeconds(), 0d); first = new Duration(5, 1000.0); second = new Duration(6, 2000.0); difference = Duration.subtract(second, first); Assert.assertEquals((int) 1, (int) difference.getDays()); Assert.assertEquals(1000.0, difference.getSeconds(), 0d); difference = Duration.subtract(first, second); Assert.assertEquals((int) -1, (int) difference.getDays()); Assert.assertEquals(-1000.0, difference.getSeconds(), 0d); first = new Duration(5, 86000.0); second = new Duration(6, 100.0); difference = Duration.subtract(second, first); Assert.assertEquals((int) 0, (int) difference.getDays()); Assert.assertEquals(500.0, difference.getSeconds(), 0d); difference = Duration.subtract(first, second); Assert.assertEquals((int) 0, (int) difference.getDays()); Assert.assertEquals(-500.0, difference.getSeconds(), 0d);