Java Duration Class | minus() Method with Example

Duration Class minus() method: Here, we are going to learn about the minus() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020

Duration Class minus() method

Syntax:

    public Duration minus(Duration d);
    public Duration minus(long amt, TemporalUnit t_unit);
  • minus() method is available in java.time package.
  • minus(Duration d) method is used to subtract the given Duration from this Duration and returns the Duration.
  • minus(long amt, TemporalUnit t_unit) method is used to subtract the given amount in the given units from this Duration and return the Duration.
  • These methods may throw an exception at the time of performing subtraction.
    ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, minus(Duration d),
    • Duration d – represents the Duration to be subtracted from this Duration.
  • In the first case, minus(long amt, TemporalUnit t_unit),
    • long amt – represents the amount in units to be subtracted from this Duration.
    • TemporalUnit t_unit – represents the unit to measure the given amount.

Return value:

In both the cases, the return type of the method is Duration,

  • In the first case, it returns the Duration that holds the value subtracted the given duration from this Duration.
  • In the second case, it returns the Duration that holds the value subtracted the given amount in a unit from this Duration.

Example:

// Java program to demonstrate the example 
// of minus() method of Duration

import java.time.*;
import java.time.temporal.*;

public class MinusOfDuration {
    public static void main(String args[]) {
        long amt = 10;

        // Instantiates two Duration objects
        Duration du1 = Duration.ofMinutes(3);
        Duration du2 = Duration.parse("P2DT2H20M20S");

        // Display du1, du2 
        System.out.println("du1: " + du1);
        System.out.println("du2: " + du2);
        System.out.println("amt to subtract: " + amt);

        System.out.println();

        // subtracts the given duration 
        // from this Duration du2 and returns
        // the Duration i.e. here we subtract
        // the given 3 minutes from this du2 that holds
        // the value of 2D:2H:20M:20S
        Duration minus_val = du2.minus(du1);

        // Display minus_val
        System.out.println("du2.minus(du1): " + minus_val);

        // subtracts the given amount 
        // in the given unit from this Duration 
        // and returns the Duration i.e. 
        // here we are subtracting the amt
        // 10 in minutes unit from this du2
        // that holds the value of 2D:2H:20M:20S
        minus_val = du2.minus(amt, ChronoUnit.MINUTES);

        // Display minus_val
        System.out.println("du2.minus(amt,ChronoUnit.MINUTES): " + minus_val);
    }
}

Output

du1: PT3M
du2: PT50H20M20S
amt to subtract: 10

du2.minus(du1): PT50H17M20S
du2.minus(amt,ChronoUnit.MINUTES): PT50H10M20S



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.