What is the Duration.minusDays() method in Java?
Overview
The minusDays() is an instance method of the Duration class, which is used to subtract the specified duration, in standard 24 hours, from the Duration object.
The number of days is multiplied by
86400to obtain the number of seconds to subtract. This is based on the standard definition of a day which is 24 hours.
How to import Duration class
The minusDays() method is defined in the Duration class, which in turn is defined in the java.time package. To import the Duration class check the following import statement:
import java.time.Duration;
Syntax
public Duration minusDays(long daysToSubtract)
Parameters
long daysToSubtract: The number of days to subtract. Can be positive or negative.
Return value
This method returns a new instance of the Duration class with the number of days subtracted.
Code
In the below code, we subtract three days from the baseDuration object with the help of the minusDays() method and print the new object to the console:
import java.time.Duration;public class Main{public static void main(String[] args) {// Define a duration objectDuration baseDuration = Duration.ofDays(5);// number of days to subtractint daysToSubtract = 3;// New Duration object after the subtractionDuration newDuration = baseDuration.minusDays(daysToSubtract);System.out.printf("%s - %s days = %s", baseDuration, daysToSubtract, newDuration);System.out.println();}}