可以使用getMonth()Java中MonthDay类中的方法来获取特定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());