Java 中 Period() 方法


可以使用 Java 中 Period 類中的 of() 方法,使用給定的天數、月份和年份獲取 Period。此方法需要 3 個引數,即天數、月數和年數。此外,它會返回具有給定天數、月數和年份的 Period 物件。

演示此方法的程式如下

示例

 線上演示

import java.time.Period;
public class Demo {
   public static void main(String[] args) {
      int days = 20;
      int months = 11;
      int years = 3;
      Period p = Period.of(years, months, days);
      System.out.println("The Period is: " + p);
   }
}

輸出

The Period is: P3Y11M20D

現在讓我們瞭解一下以上程式。

可以使用 of() 方法獲取具有給定天數、月數和年份的 Period,然後對其進行列印。演示此方法的程式碼片段如下

int days = 20;
int months = 11;
int years = 3;
Period p = Period.of(years, months, days);
System.out.println("The Period is: " + p);

更新日期:30-Jul-2019

85 次瀏覽

開啟你的 職業 生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.