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
86400
to obtain the number of seconds to subtract. This is based on the standard definition of a day which is 24 hours.
Duration
classThe 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;
public Duration minusDays(long daysToSubtract)
long daysToSubtract
: The number of days to subtract. Can be positive or negative.This method returns a new instance of the Duration
class with the number of days subtracted.
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();}}