ChronoPeriod multipliedBy() method in Java with Examples

The multipliedBy() method of ChronoPeriod interface in Java is used to return a new instance of ChronoPeriod after multiplying ‘X’ (a scalar quantity) each element of the period YEAR, MONTH, DAY from a given period. This function operate only on all three YEAR, MONTHS, DAYS.

Syntax:

public ChronoPeriod multipliedBy(int toMultiply)

Parameters: This method accepts a single parameter toMultiply which is the scalar number to be multiplied to the period.



Return Value: This method returns a new instance of ChronoPeriod after multiplying each element of the period with the given toMultiply input.

Exceptions: It throws an ArithmeticException.This exception is caught if numeric overflow occurs.

Below is the implementation of the above method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function multipliedBy()
// to multiply the given number to given period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to multiply a constant to given periods
    static void multiply(ChronoPeriod p1, int toMultiply)
    {
        System.out.println(p1.multipliedBy(toMultiply));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        int toMultiply = 2;
  
        multiply(p1, toMultiply);
    }
}

chevron_right


Output:

P8Y22M20D

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function multipliedBy()
// to multiply the given number to given period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to multiply a constant to given periods
    static void multiply(ChronoPeriod p1, int toMultiply)
    {
        System.out.println(p1.multipliedBy(toMultiply));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining first period
        int year = -4;
        int months = -11;
        int days = -10;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        int toMultiply = 2;
  
        multiply(p1, toMultiply);
    }
}

chevron_right


Output:

P-8Y-22M-20D

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#multipliedBy-int-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.