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

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

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

ChronoField 枚举checkValidValue()方法用于检查作为参数传递的值对于 ChronoField 常量是否有效。

句法:

public long checkValidValue(long value)

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

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

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

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

方案二:

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

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