Java中的MonthDay get()方法
可以使用get()Java中MonthDay类中的方法获取MonthDay中指定字段的值。此方法需要一个参数,即ChronoField,它从MonthDay返回指定字段的值。
演示此的程序如下所示-
示例
import java.time.*;
import java.time.temporal.*;
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_OF_YEAR is: " + md.get(ChronoField.MONTH_OF_YEAR));
}
}输出结果
The MonthDay is: --02-22 The MONTH_OF_YEAR is: 2
现在让我们了解上面的程序。
首先,显示MonthDay对象。然后使用该get()方法从MonthDay对象获取ChronoFieldMONTH_OF_YEAR的值并进行打印。演示这的代码片段如下-
MonthDay md = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md);
System.out.println("The MONTH_OF_YEAR is: " + md.get(ChronoField.MONTH_OF_YEAR));