Java的.time.temporal.ValueRange类在Java中
ValueRange 类捕获 TemporalField 实例的值的有效范围。给定的类提供范围的最小值和最大值。
注意:外部范围内可能存在无效值。例如,一个字段可能具有 1、2、3、6、7 的有效值,因此其范围为“1-7”,尽管值 4 和 5 无效。
类声明:
public final class ValueRange
extends Object
implements Serializable
ValueRange 类从类Java.lang.Object 继承以下方法:
- 克隆()
- 完成()
- 获取类()
- 通知()
- 通知所有()
- 等待()
ValueRange 类的方法:Method Description checkValidIntValue(long value, TemporalField field) This method checks that the specified value is valid and fits in an int. checkValidValue(long value, TemporalField field) This method checks that the specified value is valid. equals(Object obj) This method checks if this range is equal to another range. getLargestMinimum() This method gets the largest possible minimum value that the field can take. getMaximum() This method gets the maximum value that the field can take. getMinimum() This method gets the minimum value that the field can take. getSmallestMaximum() This method gets the smallest possible maximum value that the field can take. hashCode() This method returns a suitable hash code for this range. isFixed() This method returns true if the set of values is fixed. isIntValue() This method checks if all values in the range fit in an int. isValidIntValue(long value) This method checks if the value is within the valid range and that all values in the range fit in an int. isValidValue(long value) This method checks if the value is within the valid range. of(long min, long max) This method obtains a fixed value range. of(long min, long maxSmallest, long maxLargest) This method obtains a variable value range. of(long minSmallest, long minLargest, long maxSmallest, long maxLargest) This method obtains a fully variable value range. toString() This method a string representation of this range, not null.
示例 1:
Java
// Java program to demonstrate
// ValueRange Class and its methods
import java.time.temporal.ValueRange;
public class GFG {
public static void main(String[] args)
{
// create ValueRange object
ValueRange vRange = ValueRange.of(5555, 1000000);
// store the minimum value that the field can take
long minVal = vRange.getMinimum();
// store the maximum value that the field can take
long maxVal = vRange.getMaximum();
// print
System.out.println("Minimum value is: " + minVal);
System.out.println("Maximum value is: " + maxVal);
}
}
Java
// Java program to demonstrate
// ValueRange Class and its methods
import java.time.temporal.ValueRange;
public class GFG {
public static void main(String[] args)
{
// create ValueRange object
ValueRange vRange = ValueRange.of(1, 10000);
// check value 6001 in range or not
long value1 = vRange.checkValidValue(6001, null);
// print
System.out.println("Value passed: " + value1);
}
}
Minimum value is: 5555
Maximum value is: 1000000
示例 2:
Java
// Java program to demonstrate
// ValueRange Class and its methods
import java.time.temporal.ValueRange;
public class GFG {
public static void main(String[] args)
{
// create ValueRange object
ValueRange vRange = ValueRange.of(1, 10000);
// check value 6001 in range or not
long value1 = vRange.checkValidValue(6001, null);
// print
System.out.println("Value passed: " + value1);
}
}
Value passed: 6001