Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Duration.minus() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is minus()?

minus() is an instance method of the Duration class which is used to subtract a Duration object from another Duration object.

Defining the method

The minus() 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;

Syntax


public Duration minus(Duration duration)

Parameters

Duration duration: The duration object to subtract.

Return value

This method returns a new Duration object with the specified duration subtracted.

Code

import java.time.Duration;

public class Main{

    public static void main(String[] args) {
        Duration duration = Duration.ofHours(100);
        Duration durationToBeSubtracted = Duration.ofDays(4);
        Duration durationDifference = duration.minus(durationToBeSubtracted);
        System.out.printf("%s - %s = %s", duration, durationToBeSubtracted, durationDifference);
    }
}
Subtracting durations

Explanation

  • Line 1: We import the Duration class.
  • Line 6: We define a Duration object of 100 hours called duration.
  • Line 7: We define a Duration object of 4 days called durationToBeSubtracted which is to be subtracted from the duration object created in line 6.
  • Line 8: We subtract the duration from durationToBeAdded with the help of the minus() method on the duration object.
  • Line 9: We print the objects.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring