Java中ChronoPeriod接口的multipliedBy()方法用于在将给定期间YEAR,MONTH,DAY期间的每个元素乘以“ X”(标量)后,返回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
程序2:
// 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-
相关用法
- Java Period multipliedBy()用法及代码示例
- Java Duration multipliedBy(long)用法及代码示例
- Java ChronoPeriod plus()用法及代码示例
- Java ChronoPeriod get()用法及代码示例
- Java ChronoPeriod between()用法及代码示例
- Java ChronoPeriod getChronology()用法及代码示例
- Java ChronoPeriod negated()用法及代码示例
- Java ChronoPeriod negated()用法及代码示例
- Java ChronoPeriod equals()用法及代码示例
- Java ChronoPeriod toString()用法及代码示例
- Java ChronoPeriod minus()用法及代码示例
- Java ChronoPeriod subtractFrom()用法及代码示例
- Java ChronoPeriod addTo()用法及代码示例
- Java ChronoPeriod hashCode()用法及代码示例
- Java ChronoPeriod isZero()用法及代码示例
注:本文由纯净天空筛选整理自srinam大神的英文原创作品 ChronoPeriod multipliedBy() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。