Period multipliedBy() method in Java


An immutable copy of a Period where all the Period elements are multiplied by a value can be obtained using the method multipliedBy() in the Period class in Java. This method requires a single parameter i.e. the value which is to be multiplied and it returns the immutable copy of the Period which is multiplied by a value.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.Period;
public class Demo {
   public static void main(String[] args) {
      String period = "P5Y9M4D";
      Period p = Period.parse(period);
      System.out.println("The Period is: " + p);
      System.out.println("The Period elements multiplied by 3 are: " + p.multipliedBy(3));
   }
}

Output

The Period is: P5Y9M4D
The Period elements multiplied by 3 are: P15Y27M12D

Now let us understand the above program.

First the Period is displayed. Then an immutable copy of the Period where 3 is multiplied to the elements is obtained using the multipliedBy() method and this is displayed. A code snippet that demonstrates this is as follows:

String period = "P5Y9M4D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The Period elements multiplied by 3 are: " + p.multipliedBy(3));

Updated on: 30-Jul-2019

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements