Java Duration Class | multipliedBy() Method with Example

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

Duration Class multipliedBy() method

  • multipliedBy() method is available in java.time package.
  • multipliedBy() method is used to multiply this Duration by the given value.
  • multipliedBy() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • multipliedBy() method may throw an exception at the time of performing subtraction.
    ArithmeticException: This exception may throw when the calculated result value exceeds the limit.

Syntax:

    public Duration multipliedBy(long val);

Parameter(s):

  • long val – represents the value is to be multiplied with this Duration.

Return value:

The return type of this method is Duration, it returns the Duration that holds the value of this Duration multiplied by the given value.

Example:

// Java program to demonstrate the example 
// of multipliedBy(long val) method of Duration

import java.time.*;

public class MultipliedByOfDuration {
    public static void main(String args[]) {
        long mul_val = 5;

        // Instantiates two Duration objects
        Duration du1 = Duration.ofHours(10);
        Duration du2 = Duration.ofMinutes(25);

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

        System.out.println("Multiplicand: " + mul_val);

        // multiplies this Duration object(du1)
        // by the given value (mul_val) 

        Duration multiplied_val = du1.multipliedBy(mul_val);
        System.out.println("du1.multipliedBy(mul_val): " + multiplied_val);

        // multiplies this Duration object(du2)
        // by the given value (mul_val) 
        multiplied_val = du2.multipliedBy(mul_val);
        System.out.println("du2.multipliedBy(mul_val): " + multiplied_val);
    }
}

Output

du1: PT10H
du2: PT25M
Multiplicand: 5
du1.multipliedBy(mul_val): PT50H
du2.multipliedBy(mul_val): PT2H5M


Comments and Discussions!

Load comments ↻





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