MonthDay 在 Java 中的 getMonth 方法


可以使用 Java 中 MonthDay 類中的 getMonth() 方法獲取特定 MonthDay 的月名稱。此方法不需要引數,它返回年中月份的名稱。

演示此功能的一個程式如下所示

示例

 線上演示

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.getMonth());
   }
}

輸出

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

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

首先顯示 MonthDay。然後使用 getMonth() 方法顯示 MonthDay 的月份名稱。一個演示此功能的程式碼片段如下所示

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

更新於:2019-07-30

183 瀏覽量

開啟您的職業生涯

完成課程以獲得認證

開始學習
廣告