Open In App

java.time.temporal.TemporalQueries Class in Java

Last Updated : 28 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads