當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ChronoPeriod multipliedBy()用法及代碼示例


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-



相關用法


注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoPeriod multipliedBy() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。