minusSeconds()
is an instance method of the Duration
class, which subtracts the specified duration in seconds from the Duration
object.
The minusSeconds()
method is defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class, check the following import statement:
import java.time.Duration;
public Duration minusSeconds(long secondsToSubtract)
long secondsToSubtract
: The number of seconds to subtract. The number can be positive or negative.This method returns a new instance of the Duration
class with the number of seconds subtracted.
In the code below, we subtract 100
seconds from the baseDuration
object with the help of the minusSeconds()
method and print the new object to the console.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration baseDuration = Duration.ofMinutes(3);int secondsToSubtract = 100;Duration newDuration = baseDuration.minusSeconds(secondsToSubtract);System.out.printf("%s - %s seconds = %s", baseDuration, secondsToSubtract, newDuration);System.out.println();}}