R - 从查询返回的类型
@FunctionalInterface public interface TemporalQuery<R>
查询是从时间对象提取信息的关键工具。 它们存在于外部化查询过程,根据策略设计模式允许不同的方法。 示例可能是一个查询,检查日期是闰年前的二月二十九日前一天,还是计算下一个生日的天数。
TemporalField接口提供了另一种用于查询时间对象的机制。 该接口限于返回一个long 。 相比之下,查询可以返回任何类型。
有使用的两种等价的方式TemporalQuery 。 第一个是直接在这个接口上调用方法。 二是使用TemporalAccessor.query(TemporalQuery) :
// these two lines are equivalent, but the second approach is recommended
temporal = thisQuery.queryFrom(temporal);
temporal = temporal.query(thisQuery);
建议使用第二种方法, query(TemporalQuery) ,因为在代码中阅读更清楚。
最常见的实现是方法引用,例如LocalDate::from和ZoneId::from 。 其他常见的查询提供作为静态方法TemporalQueries 。
| Modifier and Type | Method and Description |
|---|---|
R |
queryFrom(TemporalAccessor temporal)
查询指定的时间对象。
|
R queryFrom(TemporalAccessor temporal)
这将查询指定的时间对象以使用实现类中封装的逻辑返回对象。 示例可能是一个查询,检查日期是闰年前的二月二十九日前一天,还是计算下一个生日的天数。
使用这种方法有两种等效的方法。 第一个是直接调用这个方法。 二是使用TemporalAccessor.query(TemporalQuery) :
// these two lines are equivalent, but the second approach is recommended
temporal = thisQuery.queryFrom(temporal);
temporal = temporal.query(thisQuery);
建议使用第二种方法, query(TemporalQuery) ,因为在代码中阅读更清楚。
TemporalAccessor上的任何方法来确定结果。
输入对象不能更改。
输入的时间对象可以在不同于ISO的日历系统中。 实现可以选择记录与其他日历系统的兼容性,或者通过querying the chronology拒绝非ISO临时对象。
可以从多个并行线程调用此方法。 调用时必须是线程安全的。
temporal - 要查询的时间对象,不为空
DateTimeException - 如果无法查询
ArithmeticException - 如果发生数字溢出
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.