📜  Java中的 ChronoField checkValidIntValue() 方法及示例

📅  最后修改于: 2022-05-13 01:54:29.773000             🧑  作者: Mango

Java中的 ChronoField checkValidIntValue() 方法及示例

ChronoField 枚举checkValidIntValue()方法用于检查作为参数传递的值是否对 ChronoField 常量有效,以及值是否适合整数。

句法:

public int checkValidIntValue(long value)

参数:此方法接受要检查的

返回值:该方法返回传入的值。

下面的程序说明了 ChronoField.checkValidIntValue() 方法:
方案一:

// Java program to demonstrate
// ChronoField.checkValidIntValue() method
  
import java.time.temporal.ChronoField;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get chronoField
        ChronoField chronoField
            = ChronoField.valueOf("HOUR_OF_DAY");
  
        // apply checkValidIntValue()
        int validInt
            = chronoField.checkValidIntValue(21);
  
        // print
        System.out.println("Value passed :"
                           + validInt);
    }
}
输出:
Value passed :21

方案二:

// Java program to demonstrate
// ChronoField.checkValidIntValue() method
  
import java.time.temporal.ChronoField;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get chronoField
        ChronoField chronoField
            = ChronoField.valueOf("YEAR_OF_ERA");
  
        // apply checkValidIntValue()
        int validInt
            = chronoField.checkValidIntValue(2021);
  
        // print
        System.out.println("Value passed :"
                           + validInt);
    }
}
输出:
Value passed :2021

参考资料:https: Java/time/temporal/ChronoField.html#checkValidIntValue(long)