Java中的 ChronoPeriod multipliedBy() 方法及示例
Java中ChronoPeriod 接口的multipliedBy()方法用于在从给定期间乘以“X”(一个标量)期间 YEAR、MONTH、DAY 的每个元素后返回 ChronoPeriod 的新实例。此函数仅适用于所有三个 YEAR、MONTHS、DAYS。
句法:
public ChronoPeriod multipliedBy(int toMultiply)
参数:此方法接受单个参数toMultiply ,它是要与周期相乘的标量数。
返回值:此方法在将周期的每个元素与给定的 toMultiply 输入相乘后返回 ChronoPeriod 的新实例。
异常:它抛出一个ArithmeticException 。如果发生数字溢出,则会捕获此异常。
下面是上述方法的实现:
程序 1 :
// 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);
}
}
输出:
P8Y22M20D
方案二:
// 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);
}
}
输出:
P-8Y-22M-20D
参考:https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoPeriod.html#multipliedBy-int-