LocalDate类query()方法 (LocalDate Class query() method)
query() method is available in java.time package.
query()方法在java.time包中可用。
query() method is used to query this LocalDate with the help of the given temporal query object.
query()方法用于在给定的时间查询对象的帮助下查询此LocalDate。
query() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
query()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
query() method may throw an exception at the time of querying this object.
query()方法在查询该对象时可能会引发异常。
- DateTimeException: This exception may throw when it couldn’t query this object.DateTimeException :无法查询此对象时,可能引发此异常。
- ArithmeticException: This exception may throw when the calculated result exceeds the limit.ArithmeticException :当计算结果超出限制时,可能引发此异常。
Syntax:
句法:
public Result query(TemporalQuery t_query);
Parameter(s):
参数:
TemporalQuery t_query – represents the query object by which to query this object.
TemporalQuery t_query –表示用于查询该对象的查询对象。
Return value:
返回值:
The return type of this method is Result, it returns the queried result that is defined by the given query object.
此方法的返回类型为Result ,它返回由给定查询对象定义的查询结果。
Example:
例:
// Java program to demonstrate the example
// of Result query(TemporalQuery t_query) method
// of LocalDate
import java.time.*;
import java.time.temporal.*;
public class QueryOfLocalDate {
public static void main(String args[]) {
// Instantiates two LocalDate
LocalDate l_da1 = LocalDate.parse("2007-04-04");
LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);
// Create a zone for LocalDate l_da2
ZoneId z_id = ZoneId.of("Africa/Accra");
ZonedDateTime zone = l_da2.atStartOfDay(z_id);
// Create an offset for
// LocalDate l_da1
OffsetTime off = OffsetTime.now();
OffsetDateTime da_with_off = l_da1.atTime(off);
// Display l_da1,l_da2
System.out.println("LocalDate l_da1,l_da2 : ");
System.out.println("l_da1: " + l_da1);
System.out.println("l_da2: " + l_da2);
System.out.println();
// Here, this method queries to this
// LocalDate l_da1 with the help of
// the given query i.e. here we are
// querying to this l_da1 for
// precision
String query = l_da1.query(TemporalQueries.precision()).toString();
// Display query
System.out.println("l_da1.query(TemporalQueries.precision()): " + query);
// Here, this method queries to this
// OffsetDateTime (da_with_off) with
// the help of the given query i.e.
// here we are querying to this da_with_off for
// offset
query = da_with_off.query(TemporalQueries.offset()).toString();
// Display query
System.out.println("da_with_off.query(TemporalQueries.offset()): " + query);
// Here, this method queries to this zone
// with the help of the given query i.e.
// here we are querying to this zone for
// zone
query = zone.query(TemporalQueries.zone()).toString();
// Display query
System.out.println("zone.query(TemporalQueries.zone()): " + query);
}
}
Output
输出量
LocalDate l_da1,l_da2 :
l_da1: 2007-04-04
l_da2: 2008-02-06
l_da1.query(TemporalQueries.precision()): Days
da_with_off.query(TemporalQueries.offset()): Z
zone.query(TemporalQueries.zone()): Africa/Accra
翻译自: https://www.includehelp.com/java/localdate-query-method-with-example.aspx