Thursday, October 23, 2025
HomeLanguagesJavaPeriod multipliedBy() method in Java with Examples

Period multipliedBy() method in Java with Examples

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

Syntax:

public Period 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 Period 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:




// Java code to show the function multipliedBy()
// to multiply the given number to given period
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodClass {
  
    // Function to multiply a constant to given periods
    static void multiply(Period 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;
        Period p1 = Period.of(year, months, days);
  
        int toMultiply = 2;
  
        multiply(p1, toMultiply);
    }
}


Program 2:




// Java code to show the function multipliedBy()
// to multiply the given number to given period
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodClass {
  
    // Function to multiply a constant to given periods
    static void multiply(Period 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;
        Period p1 = Period.of(year, months, days);
  
        int toMultiply = 2;
  
        multiply(p1, toMultiply);
    }
}


Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#multipliedBy-int-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS