Java ZonedDateTime query()方法及示例
ZonedDateTime 类的 query() 方法用于使用指定的查询作为参数来查询这个ZonedDateTime。作为参数传递的TemporalQuery对象定义了用于从这个ZonedDateTime获得结果的逻辑。
语法
public <R> R query(TemporalQuery<R> query)
参数: 该方法只接受一个参数 query ,它是要调用的查询。
返回值 :该方法返回查询结果,可能返回空值。
异常: 该方法抛出以下异常。
- DateTimeException – 如果无法查询。
- ArithmeticException – 如果发生数字溢出。
下面的程序说明了query()方法。
程序1 :
// Java program to demonstrate
// ZonedDateTime.query() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create ZonedDateTime object
ZonedDateTime zlt
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// apply the query method of ZonedDateTime class
String value
= zlt.query(
TemporalQueries.precision())
.toString();
// print the result
System.out.println("Precision value"
+ " for ZonedDateTime is "
+ value);
}
}
输出。
Precision value for ZonedDateTime is Nanos
程序2
// Java program to demonstrate
// ZonedDateTime.query() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create ZonedDateTime object
ZonedDateTime zlt
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// apply query method of ZonedDateTime class
// print the result
System.out.println("offset value for "
+ "ZonedDateTime is "
+ zlt.query(
TemporalQueries.offset()));
}
}
输出。
offset value for ZonedDateTime is +02:00
参考资料: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#query(java.time.temporal.TemporalQuery)