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