可以使用getDayOfMonth()Java中MonthDay类中的方法来获取特定MonthDay的月份。此方法不需要任何参数,它返回一个月中的某天,该范围可以在1到31之间。
演示此程序如下
示例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 day of the month is: " + md.getDayOfMonth());
}
}
输出结果The MonthDay is: --02-22
The day of the month is: 22
现在让我们了解上面的程序。
首先显示MonthDay。然后使用该getDayOfMonth()方法显示MonthDay的月份。演示此代码段如下所示:MonthDay md = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md);
System.out.println("The day of the month is: " + md.getDayOfMonth());