Java中的LocalTime isSupported()方法

可以使用isSupported()Java中LocalTime类中的方法来检查LocalTime类是否支持ChronoUnit 。此方法需要一个参数,即ChronoUnit进行检查。如果LocalTime类支持ChronoUnit,则返回true,否则返回false。

演示此的程序如下所示-

示例

import java.time.*;
import java.time.temporal.*;
public class Demo {
   public static void main(String[] args) {
      LocalTime lt = LocalTime.now();
      System.out.println("The LocalTime is: " + lt);
      boolean flag = lt.isSupported(ChronoUnit.HOURS);
      if(flag)
         System.out.println("The ChronoUnit HOURS is supported");
      else
         System.out.println("The ChronoUnit HOURS is not supported");
   }
}

输出结果

The LocalTime is: 07:09:42.262
The ChronoUnit HOURS is supported

现在让我们了解上面的程序。

首先,显示当前的LocalTime对象。然后,isSupported()使用该方法检查LocalTime类是否支持ChronoUnit HOURS。返回的值存储在标志中,并打印相应的响应。演示这的代码片段如下-

LocalTime lt = LocalTime.now();
System.out.println("The LocalTime is: " + lt);
boolean flag = lt.isSupported(ChronoUnit.HOURS);
if(flag)
   System.out.println("The ChronoUnit HOURS is supported");
else
   System.out.println("The ChronoUnit HOURS is not supported");