Java的.time.temporal.TemporalQueries类在Java中
TemporalQueries类提供查询时态对象的常见实现。该实现定义了查询的逻辑。查询是从时间对象中检索信息的关键工具。例如,检查日期是否为闰年中的 2 月 29 日之前的一天,或确定时间是否介于工作时间之间的查询。
有两种使用 TemporalQuery 的等效方法:
1 、直接调用这个接口上的方法:
temporal = thisQuery.queryFrom(temporal);
2.使用TemporalAccessor.query(TemporalQuery):
temporal = temporal.query(thisQuery);
类声明:
public final class TemporalQueries extends Object
TemporalQueries 类从类Java.lang.Object 继承以下方法:
- 克隆()
- 等于()
- 完成()
- 获取类()
- 哈希码()
- 通知()
- 通知所有()
- toString()
- 等待()
TemporalQueries 类的方法:Method Description chronology() This method returns a query for the Chronology. localDate() This method returns a query for LocalDate returning null if not found. localTime() This method returns a query for LocalTime returning null if not found. offset() This method returns a query for ZoneOffset returning null if not found. precision() This method returns a query for the smallest supported unit. zone() This method returns a lenient query for the ZoneId, falling back to the ZoneOffset. zoneId() This method returns a lenient query for the ZoneId, falling back to the ZoneOffset.
Java
// Java program to demonstrate
// TemporalQueries Class
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
public class GFG {
public static void main(String[] args)
{
// creating a query to obtainsmallest supported unit
// of a temporal
TemporalQuery precision
= TemporalQueries.precision();
System.out.println("TemporalQueries precision: ");
// print smallest precision of local date
System.out.println(
LocalDate.now().query(precision));
// print smallest precision of local time
System.out.println(
LocalTime.now().query(precision));
// print smallest precision of local date-time
System.out.println(
LocalDateTime.now().query(precision));
// print smallest precision of year-month
System.out.println(
YearMonth.now().query(precision));
// print smallest precision of year
System.out.println(Year.now().query(precision));
}
}
TemporalQueries precision:
Days
Nanos
Nanos
Months
Years