Thursday, December 11, 2025
HomeLanguagesJavajava.time.temporal.TemporalQueries Class in Java

java.time.temporal.TemporalQueries Class in Java

The TemporalQueries Class provides common implementations of querying the temporal objects. The implementation defines the logic of the query. Queries are a key tool for retrieving information from temporal objects. For example, a query that checks if the date is the day before February 29th in a leap year, or determine if time lies between business hours or not.

There are two equivalent ways of using a TemporalQuery:

1. Invoke the method on this interface directly:

 temporal = thisQuery.queryFrom(temporal);

2. use TemporalAccessor.query(TemporalQuery):

temporal = temporal.query(thisQuery);

Class declaration:

public final class TemporalQueries extends Object

 TemporalQueries Class inherits following methods from class java.lang.Object:

  • clone()
  • equals()
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()

Methods of TemporalQueries Class:

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<TemporalUnit> 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));
    }
}


Output

TemporalQueries precision: 
Days
Nanos
Nanos
Months
Years
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7193 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS