Year类的query()方法,用于使用指定的查询作为参数来查询Year。作为参数传递的TemporalQuery对象定义了用于从Year获取结果的逻辑。
用法:
public R query(TemporalQuery query)
参数:此方法仅接受一个要调用的参数查询。
返回值:此方法返回查询结果,可能返回null。
异常:
此方法引发以下异常:
DateTimeException-如果无法查询。
ArithmeticException-如果发生数字溢出。
以下示例程序旨在说明query()方法:
示例1:
// Java program to demonstrate
// Year.query() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create Year object
Year ym = Year.of(2019);
// apply query method of Year class
String value
= ym.query(TemporalQueries
.precision())
.toString();
// print the result
System.out.println("Precision value"
+" for Year is "
+ value);
}
}
输出:
Precision value for Year is Years
示例2:显示如果查询未找到所需的对象,则返回null。
// Java program to demonstrate
// Year.query() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create Year object
Year ym = Year.of(2019);
// apply query method of Year class
// print the result
System.out.println("offset value for Year is "
+ ym.query(
TemporalQueries.offset()));
}
}
输出:
offset value for Year is null