Java 中的 Period ofMonths() 方法


可以在 Java 中的 Period 類中使用 ofMonths() 方法透過給定的月份數獲取 Period。此方法需要一個引數,即月份數,並返回帶有給定月份數的 Period 物件。

演示這一點的程式如下所示 −

示例

 即時演示

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

輸出

The Period is: P11M

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

使用 ofMonths() 方法獲取帶有給定月份數的 Period,然後對其進行列印。演示這一點的程式碼片段如下所示 −

int months = 11;
Period p = Period.ofMonths(months);
System.out.println("The Period is: " + p);

更新於: 30-7 月 -2019

174 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

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