TemporalQuery
Stay organized with collections
Save and categorize content based on your preferences.
Strategy for querying a temporal object.
Queries are a key tool for extracting information from temporal objects.
They exist to externalize the process of querying, permitting different
approaches, as per the strategy design pattern.
Examples might be a query that checks if the date is the day before February 29th
in a leap year, or calculates the number of days to your next birthday.
The TemporalField
interface provides another mechanism for querying
temporal objects. That interface is limited to returning a long
.
By contrast, queries can return any type.
There are two equivalent ways of using a TemporalQuery
.
The first is to invoke the method on this interface directly.
The second is to use TemporalAccessor.query(TemporalQuery)
:
// these two lines are equivalent, but the second approach is recommended
temporal = thisQuery.queryFrom(temporal);
temporal = temporal.query(thisQuery);
It is recommended to use the second approach,
query(TemporalQuery)
,
as it is a lot clearer to read in code.
The most common implementations are method references, such as
LocalDate::from
and ZoneId::from
.
Additional common queries are provided as static methods in TemporalQueries
.
Public Methods
public
abstract
R
queryFrom
(TemporalAccessor temporal)
Queries the specified temporal object.
This queries the specified temporal object to return an object using the logic
encapsulated in the implementing class.
Examples might be a query that checks if the date is the day before February 29th
in a leap year, or calculates the number of days to your next birthday.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use TemporalAccessor.query(TemporalQuery)
:
// these two lines are equivalent, but the second approach is recommended
temporal = thisQuery.queryFrom(temporal);
temporal = temporal.query(thisQuery);
It is recommended to use the second approach,
query(TemporalQuery)
,
as it is a lot clearer to read in code.
Parameters
temporal |
the temporal object to query, not null |
Returns
- the queried value, may return null to indicate not found
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eTemporalQuery\u003c/code\u003e provides a strategy for extracting information from temporal objects, offering flexibility beyond \u003ccode\u003eTemporalField\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt allows for diverse query implementations, returning any data type, including custom objects or primitives.\u003c/p\u003e\n"],["\u003cp\u003eWhile there are two usage approaches (direct invocation or via \u003ccode\u003eTemporalAccessor.query\u003c/code\u003e), the latter is recommended for code clarity.\u003c/p\u003e\n"],["\u003cp\u003eCommon implementations often leverage method references or are accessed as static methods within \u003ccode\u003eTemporalQueries\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equeryFrom\u003c/code\u003e method handles the querying logic, potentially returning null if the desired information isn't found.\u003c/p\u003e\n"]]],[],null,["# TemporalQuery\n\npublic interface **TemporalQuery** \nStrategy for querying a temporal object.\n\n\nQueries are a key tool for extracting information from temporal objects.\nThey exist to externalize the process of querying, permitting different\napproaches, as per the strategy design pattern.\nExamples might be a query that checks if the date is the day before February 29th\nin a leap year, or calculates the number of days to your next birthday.\n\n\nThe [TemporalField](../../../../reference/java/time/temporal/TemporalField.html) interface provides another mechanism for querying\ntemporal objects. That interface is limited to returning a `long`.\nBy contrast, queries can return any type.\n\n\nThere are two equivalent ways of using a `TemporalQuery`.\nThe first is to invoke the method on this interface directly.\nThe second is to use [TemporalAccessor.query(TemporalQuery)](../../../../reference/java/time/temporal/TemporalAccessor.html#query(java.time.temporal.TemporalQuery\u003cR\u003e)): \n\n```\n // these two lines are equivalent, but the second approach is recommended\n temporal = thisQuery.queryFrom(temporal);\n temporal = temporal.query(thisQuery);\n \n```\nIt is recommended to use the second approach, `query(TemporalQuery)`, as it is a lot clearer to read in code.\n\n\nThe most common implementations are method references, such as\n`LocalDate::from` and `ZoneId::from`.\nAdditional common queries are provided as static methods in [TemporalQueries](../../../../reference/java/time/temporal/TemporalQueries.html). \n\n### Public Method Summary\n\n|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract R | [queryFrom](../../../../reference/java/time/temporal/TemporalQuery.html#queryFrom(java.time.temporal.TemporalAccessor))([TemporalAccessor](../../../../reference/java/time/temporal/TemporalAccessor.html) temporal) Queries the specified temporal object. |\n\nPublic Methods\n--------------\n\n#### public abstract R\n**queryFrom**\n([TemporalAccessor](../../../../reference/java/time/temporal/TemporalAccessor.html) temporal)\n\nQueries the specified temporal object.\n\n\nThis queries the specified temporal object to return an object using the logic\nencapsulated in the implementing class.\nExamples might be a query that checks if the date is the day before February 29th\nin a leap year, or calculates the number of days to your next birthday.\n\n\nThere are two equivalent ways of using this method.\nThe first is to invoke this method directly.\nThe second is to use [TemporalAccessor.query(TemporalQuery)](../../../../reference/java/time/temporal/TemporalAccessor.html#query(java.time.temporal.TemporalQuery\u003cR\u003e)): \n\n```\n // these two lines are equivalent, but the second approach is recommended\n temporal = thisQuery.queryFrom(temporal);\n temporal = temporal.query(thisQuery);\n \n```\nIt is recommended to use the second approach, `query(TemporalQuery)`, as it is a lot clearer to read in code.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| temporal | the temporal object to query, not null |\n|----------|----------------------------------------|\n\n##### Returns\n\n- the queried value, may return null to indicate not found \n\n##### Throws\n\n| [DateTimeException](../../../../reference/java/time/DateTimeException.html) | if unable to query |\n| [ArithmeticException](../../../../reference/java/lang/ArithmeticException.html) | if numeric overflow occurs |\n|---------------------------------------------------------------------------------|----------------------------|"]]