MonthDay 的 getMonthValue() 方法在 Java 中


在 Java 中的 MonthDay 類中,使用 getMonthValue() 方法可以獲取一年中的月份。此方法不需要引數,並且會返回一年中的月份,其範圍可能在 1 到 12 之間。

展示此方法的程式如下所示

範例

 現場演示

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      MonthDay md = MonthDay.parse("--02-22");
      System.out.println("The MonthDay is: " + md);
      System.out.println("The month is: " + md.getMonthValue());
   }
}

輸出

The MonthDay is: --02-22
The month is: 2

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

首先,顯示 MonthDay。然後,使用 getMonthValue() 方法獲取一年中的月份,並顯示出來。展示此方法的程式碼段如下所示

MonthDay md = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md);
System.out.println("The month is: " + md.getMonthValue());

更新時間: 30-07-2019

62 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告