Java中的 ChronoUnit isTimeBased() 方法及示例
ChronoUnit 枚举的isTimeBased()方法用于检查此 ChronoUnit 是否为时间单位。从纳秒到半天的所有 ChronoUnit 都是基于时间的。
句法:
public boolean isTimeBased()
参数:此方法不接受任何内容。
返回值:如果是时间单位,该方法返回true ,如果是数据单位,则返回false 。
下面的程序说明了 ChronoUnit.isTimeBased() 方法:
方案一:
// Java program to demonstrate
// ChronoUnit.isTimeBased() method
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
// get ChronoUnit
ChronoUnit chronoUnit
= ChronoUnit.valueOf("NANOS");
// apply isTimeBased()
boolean isTimeBasedAttribute
= chronoUnit.isTimeBased();
// print
System.out.println(
"NANOS"
+ " is Date based attribute:"
+ isTimeBasedAttribute);
}
}
输出:
NANOS is Date based attribute:true
方案二:
// Java program to demonstrate
// ChronoUnit.isTimeBased() method
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
// get ChronoUnit
ChronoUnit chronoUnit
= ChronoUnit.valueOf("DAYS");
// apply isTimeBased()
boolean isTimeBasedAttribute
= chronoUnit.isTimeBased();
// print
System.out.println(
"DAYS"
+ " is Date based attribute:"
+ isTimeBasedAttribute);
}
}
输出:
DAYS is Date based attribute:false
参考资料:https: Java/time/temporal/ChronoUnit.html#isTimeBased()