Java中的MonthDay isSupported()方法
可以使用isSupported()
Java中MonthDay类中的方法来检查MonthDay类是否支持ChronoField。此方法需要单个参数,即ChronoField进行检查。如果MonthDay类支持ChronoField,则返回true,否则返回false。
演示此程序如下
示例
import java.time.*; import java.time.temporal.*; public class Demo { public static void main(String[] args) { MonthDay md = MonthDay.now(); System.out.println("The MonthDay is: " + md); boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR); if(flag) System.out.println("The ChronoField MONTH_OF_YEAR is supported"); else System.out.println("The ChronoField MONTH_OF_YEAR is not supported"); } }
输出
The MonthDay is: --02-21 The ChronoField MONTH_OF_YEAR is supported
现在让我们了解上面的程序。
首先,显示当前的MonthDay对象。然后,该isSupported()
方法用于检查MonthDay类是否支持ChronoFieldMONTH_OF_YEAR。返回的值存储在标志中,并打印相应的响应。演示此代码段如下所示:
MonthDay md = MonthDay.now(); System.out.println("The MonthDay is: " + md); boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR); if(flag) System.out.println("The ChronoField MONTH_OF_YEAR is supported"); else System.out.println("The ChronoField MONTH_OF_YEAR is not supported");