Java 中的 Period ofYears() 方法


可以使用 Java 中 `Period` 類中的 `ofYears()` 方法獲取給定年數的時間段。此方法需要一個引數,即年數,並返回具有給定年數的時間段物件。

以下是演示的程式 −

示例

 即時演示

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

輸出

The Period is: P3Y

現在讓我們瞭解上述程式。

使用 `ofYears()` 方法獲取指定年數的時間段,然後列印。以下程式碼片段演示了這一點 −

int years = 3;
Period p = Period.ofYears(years);
System.out.println("The Period is: " + p);

更新於: 2019-07-30

152 次瀏覽

開啟你的 職業生涯

完成課程並獲得證書

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