Java中Period類的multipliedBy()方法用於在將給定期間的YEAR,MONTH,DAY期間的每個元素乘以“ X”(標量)後,返回Period的新實例。此函數僅在YEAR,MONTHS和DAYS這三個日期都有效。
用法:
public Period multipliedBy(int toMultiply)
參數:此方法接受toMultiply的單個參數,該參數是要乘以周期的標量數。
返回值:在將周期的每個元素與給定的toMultiply輸入相乘之後,此方法返回Period的新實例。
異常:引發ArithmeticException。如果發生數值溢出,則會捕獲此異常。
下麵是上述方法的實現:
程序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);
}
}
程序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);
}
}
參考: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#multipliedBy-int-
相關用法
- Java ChronoPeriod multipliedBy()用法及代碼示例
- Java Duration multipliedBy(long)用法及代碼示例
- Java Period between()用法及代碼示例
- Java Period get()用法及代碼示例
- Java Period plus()用法及代碼示例
- Java Period of()用法及代碼示例
- Java Period from()用法及代碼示例
- Java Period isNegative()用法及代碼示例
- Java Period plusYears()用法及代碼示例
- Java Period normalized()用法及代碼示例
- Java Period negated()用法及代碼示例
- Java Period hashCode()用法及代碼示例
- Java Period equals()用法及代碼示例
- Java Period getUnits()用法及代碼示例
- Java Period minusYears()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period multipliedBy() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。