Java - Datetime Objects Querying

Introduction

All datetime classes support queries.

For example, you can query a LocalDate whether it is a Friday 13.

TemporalQuery<R> interface represents a query.

All datetime classes contain a query() method, which takes a TemporalQuery as a parameter and returns a result.

TemporalQueries class contains several predefined queries as its static methods.

If a datetime object does not have the information sought in the query, the query returns null.

For example, the query for a LocalDate from a LocalTime object returns null.

Chronology is an interface that is used to identify and manipulate dates in a calendar system.

The following table lists Utility Methods in the TemporalQueries Class

Method
Return Type
Description
chronology()
TemporalQuery<Chronology>
A query to get the chronology.
localDate()
TemporalQuery<LocalDate>
A query to get the LocalDate.
localTime()
TemporalQuery<LocalTime>
A query to get the LocalTime.
offset()
TemporalQuery<ZoneOffset>
A query to get the ZoneOffset.
precision()
TemporalQuery<TemporalUnit>
A query to get the smallest supported unit.
zone()


TemporalQuery<ZoneId>


A query to get the ZoneId. If the ZoneId is not available it queries
for ZoneOffset. It returns null if both are not available, for
example, a LocalDate has neither.
zoneId()
TemporalQuery<ZoneId>
A query to get the ZoneId. If ZoneId is not available, it returns null.

The following code shows how to use predefined queries.

It uses queries to get the precision and LocalDate from a LocalDate, a LocalTime, and a ZonedDateTime.

Demo

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;

public class Main {
  public static void main(String[] args) {
    // Get references of the precision and local date queries
    TemporalQuery<TemporalUnit> precisionQuery = TemporalQueries.precision();
    TemporalQuery<LocalDate> localDateQuery = TemporalQueries.localDate();

    // Query a LocalDate
    LocalDate ld = LocalDate.now();
    TemporalUnit precision = ld.query(precisionQuery);
    LocalDate queryDate = ld.query(localDateQuery);
    System.out.println("Precision of LocalDate: " + precision);
    System.out.println("LocalDate of LocalDate: " + queryDate);

    // Query a LocalTime
    LocalTime lt = LocalTime.now();
    precision = lt.query(precisionQuery);
    queryDate = lt.query(localDateQuery);
    System.out.println("Precision of LocalTime: " + precision);
    System.out.println("LocalDate of LocalTime: " + queryDate);

    // Query a ZonedDateTime
    ZonedDateTime zdt = ZonedDateTime.now();
    precision = zdt.query(precisionQuery);
    queryDate = zdt.query(localDateQuery);
    System.out.println("Precision of ZonedDateTime: " + precision);
    System.out.println("LocalDate of ZonedDateTime: " + queryDate);
  }/*w  w  w  . j  a  v  a 2s.  c  om*/
}

Result

Related Topics