o7planning

Java TemporalQueries Tutorial with Examples

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- TemporalQueries

The TemporalQueries class provides static methods to obtain common and useful TemporalQuery objects. The information received from these TemporalQuery can be:
  • Chronology
  • LocalDate
  • LocalTime
  • ZoneOffset
  • Precision
  • Zone
  • ZoneId
See also article about TemporalQuery interface with basic and custom TemporalQuery.
There are two ways to query a TemporalAccessor object, in which the second approach is recommended.

// These two lines are equivalent, but the second approach is recommended

information = temporalQuery.queryFrom(temporalAccessor);  // (1)

information = temporalAccessor.query(temporalQuery);          // (2)

2- TemporalQueries methods

All methods provided by TemporalQueries are static and return a TemporalQuery object.

public static TemporalQuery<ZoneId> zoneId()  

public static TemporalQuery<Chronology> chronology()  

public static TemporalQuery<TemporalUnit> precision()  

public static TemporalQuery<ZoneId> zone()

public static TemporalQuery<ZoneOffset> offset()  

public static TemporalQuery<LocalDate> localDate()

public static TemporalQuery<LocalTime> localTime() 

3- precision()

Return TemporalQuery<TemporalUnit> to query the smallest unit supported by a TemporalAcccessor.

public static TemporalQuery<TemporalUnit> precision()  
Table of classes that implement the TemporalAccessor interface available in the JDK and the smallest supported unit.
TemporalAccessor Precision isTimeBased() isDateBased()
Instant NANOS true false
LocalDate DAYS false true
LocalTime NANOS true false
LocalDateTime NANOS true false
ZonedDateTime NANOS true false
OffsetTime NANOS true false
OffsetDateTime NANOS true false
ChronoLocalDate DAYS false true
ChronoLocalDateTime NANOS true false
ChronoZonedDateTime NANOS true false
Era ERAS false true
DayOfWeek DAYS false true
Month MONTHS false true
Year YEARS false true
YearMonth MONTHS false true
MonthDay null    
ZoneOffset null    
Example:
TemporalQueries_precision_ex1.java

LocalDate localDate = LocalDate.now();

TemporalQuery<TemporalUnit> query = TemporalQueries.precision();

TemporalUnit smallestUnit = localDate.query(query); // Can cast to ChronoUnit
ChronoUnit smallestChronoUnit = (ChronoUnit) smallestUnit; // ChronoUnit.DAYS

System.out.println("localDate support smallest unit: " + smallestChronoUnit); // Days
System.out.println(" >> Name: " + smallestChronoUnit.name()); // DAYS
System.out.println(" >> isTimeBased()?: " + smallestChronoUnit.isTimeBased()); // false
System.out.println(" >> isDateBased()?: " + smallestChronoUnit.isDateBased()); // true
Output:

localDate support smallest unit: Days
 >> Name: DAYS
 >> isTimeBased()?: false
 >> isDateBased()?: true

4- chronology()

Return TemporalQuery<Chronology> to obtain information about the Chronology associated with a TemporalAccessor.

public static TemporalQuery<Chronology> chronology()  
TemporalAccessor Return Note
ChronoLocalDate Returns the associated chronology  
ChronoLocalDateTime
ChronoZonedDateTime
Era
LocalDate
Return
IsoChronology.INSTANCE
 
LocalDateTime
ZonedDateTime
OffsetDateTime
Month
Year
YearMonth
MonthDay
LocalTime null Does not represent a date
OffsetTime
ZoneOffset
Instant
DayOfWeek null Shared across chronologies
TemporalQueries_chronology_ex1.java

TemporalAccessor localDateTime = LocalDateTime.now();  
Chronology chronology = localDateTime.query(TemporalQueries.chronology());

System.out.println(chronology.getClass().getName()); // java.time.chrono.IsoChronology
System.out.println(chronology.getId()); // ISO
The reference of the static method Chronology.from(TemporalAccessor) can be used as a TemporalQuery, which corresponds to this method, except it can throw an exception if the result cannot be obtained.
TemporalQueries_chronology_ex2.java

TemporalQuery<Chronology> query = Chronology::from; // Method reference

// TemporalAccessor object:
TemporalAccessor temporalAccessor = MonthDay.now();
// Throw exception if Chronology cannot be obtained.
Chronology chronology = temporalAccessor.query(query);

System.out.println(chronology.getClass().getName()); // java.time.chrono.IsoChronology
System.out.println(chronology.getId()); // ISO
Output:

java.time.chrono.IsoChronology
ISO

5- localDate()

Return a TemporalQuery<LocalDate> to retrieve the LocalDate component from a TemporalAccessor object. The query result is null if this component does not exist.

public static TemporalQuery<LocalDate> localDate()
Example:
TemporalQueries_localDate_ex1.java

TemporalQuery<LocalDate> query = TemporalQueries.localDate();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

LocalDate localDate = zonedDateTime.query(query);
System.out.println("localDate: " + localDate);
System.out.println();
//
LocalTime localTime  = LocalTime.now();
System.out.println("localTime: " + localTime);

localDate = localTime.query(query);
System.out.println("localDate: " + localDate);
Output:

zonedDateTime: 2021-07-10T01:26:20.194520+06:00[Asia/Bishkek]
localDate: 2021-07-10

localTime: 01:26:20.195471
localDate: null
The reference of the static method LocalDate.from(TemporalAccessor) can be used as a TemporalQuery, which corresponds to this method, except it may throw an exception if the result cannot be obtained.
TemporalQueries_localDate_ex2.java

// Method reference
TemporalQuery<LocalDate> query = LocalDate::from; // Same as: TemporalQueries.localDate();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

LocalDate localDate = zonedDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("localDate: " + localDate);
System.out.println();
//
LocalTime localTime  = LocalTime.now();
System.out.println("localTime: " + localTime);

localDate = localTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("localDate: " + localDate);

6- localTime()

Return a TemporalQuery<LocalTime> to retrieve the LocalTime component from a TemporalAccessor object. The query result is null if this component does not exist.

public static TemporalQuery<LocalTime> localTime()
Example:
TemporalQueries_localTime_ex1.java

TemporalQuery<LocalTime> query = TemporalQueries.localTime();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

LocalTime localTime = zonedDateTime.query(query);
System.out.println("localTime: " + localTime);
System.out.println();
//
LocalDate localDate = LocalDate.now();
System.out.println("localDate: " + localDate);

localTime = localDate.query(query);
System.out.println("localTime: " + localTime);
Output:

zonedDateTime: 2021-07-10T01:45:18.694232+06:00[Asia/Bishkek]
localTime: 01:45:18.694232

localDate: 2021-07-10
localTime: null
The reference of the static method LocalTime.from(TemporalAccessor) can be used as a TemporalQuery, which corresponds to this method, except it can throw an exception if the result cannot be obtained.
TemporalQueries_localTime_ex2.java

// Method reference
TemporalQuery<LocalTime> query = LocalTime::from; // Same as: TemporalQueries.localTime();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

LocalTime localTime = zonedDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("localTime: " + localTime);
System.out.println();
//
LocalDate localDate = LocalDate.now();
System.out.println("localDate: " + localDate);

localTime = localDate.query(query); // Throw exception if the result cannot be obtained.
System.out.println("localTime: " + localTime);

7- zoneId()

Return a TemporalQuery<ZoneId> that retrieves time zone ID information from a TemporalAccessor object. The result of this query is strict, meaning it returns a time zone ID for a region, such as "Europe/Paris", "GMT-10", "UTC+7", "UT+05:30". Time zone ID(s) based on offsets are not considered strict, such as "+08:30", "-07".​​

public static TemporalQuery<ZoneId> zoneId()  
The TemporalQuery<ZoneId> object returned from this method is only used to query the ZonedDateTime or ChroneZonedDateTime object and obtain the ZoneId associated with it. Queries with other TemporalAccessor(s) like OffsetDateTime, LocalDateTime, .. will return null.
If you need a less strict time zone query that accepts all types of time zone ID(s) including offset based time zone ID(s), use the TemporalQueries.zone() method.
TemporalQueries_zoneId_ex1.java

TemporalQuery<ZoneId> query = TemporalQueries.zoneId();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

ZoneId zoneId = zonedDateTime.query(query);
System.out.println("zoneId: " + zoneId);
System.out.println();
//
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println("offsetDateTime: " + offsetDateTime);

zoneId = offsetDateTime.query(query);
System.out.println("zoneId: " + zoneId);
Output:

zonedDateTime: 2021-07-09T23:10:49.446493+06:00[Asia/Bishkek]
zoneId: Asia/Bishkek

offsetDateTime: 2021-07-09T19:10:49.448945+02:00
zoneId: null

8- offset()

Return a TemporalQuery<ZoneOffset> to retrieve time zone offset information from a TemporalAccessor object.

public static TemporalQuery<ZoneOffset> offset()  
Example:
TemporalQueries_offset_ex1.java

TemporalQuery<ZoneOffset> query = TemporalQueries.offset();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

ZoneOffset zoneOffset = zonedDateTime.query(query);
System.out.println("zoneOffset: " + zoneOffset);
System.out.println();
//
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println("offsetDateTime: " + offsetDateTime);

zoneOffset = offsetDateTime.query(query);
System.out.println("zoneOffset: " + zoneOffset);
Output:

zonedDateTime: 2021-07-10T00:52:36.066446+06:00[Asia/Bishkek]
zoneOffset: +06:00

offsetDateTime: 2021-07-09T20:52:36.068898+02:00
zoneOffset: +02:00
The reference of the static method ZoneOffset.from(TemporalAccessor) can be used as a TemporalQuery, which corresponds to this method, except it can throw an exception if the result cannot be obtained.
TemporalQueries_offset_ex2.java

// Method reference
TemporalQuery<ZoneOffset> query = ZoneOffset::from; // Same as: TemporalQueries.offset()

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

ZoneOffset zoneOffset = zonedDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("zoneOffset: " + zoneOffset);
System.out.println();
//
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println("offsetDateTime: " + offsetDateTime);

zoneOffset = offsetDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("zoneOffset: " + zoneOffset);

9- zone()

Return a TemporalQuery<ZoneId> that retrieves time zone ID information from a TemporalAccessor object. The result of this query is lenient, meaning it accepts all ZoneId types. For example: "Europe/Paris", "GMT-10", "UTC+7", "UT+05:30", "+9:30", "-08".

public static TemporalQuery<ZoneId> zone()
This TemporalQuery queries a temporalAccessor object according to the following rule:
  1. Return TemporalQueries.zoneId().queryFrom(temporalAccessor) if it is not null.
  2. Otherwise, return TemporalQueries.offset().queryFrom(temporalAccessor).
Thus, querying a ZonedDateTime object will return zonedDateTime.getZone(), while querying an OffsetDateTime object will return offsetDateTime.getOffset().
TemporalQueries_zone_ex1.java

TemporalQuery<ZoneId> query = TemporalQueries.zone();

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

ZoneId zoneId = zonedDateTime.query(query);
System.out.println("zoneId: " + zoneId);
System.out.println();
//
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println("offsetDateTime: " + offsetDateTime);

zoneId = offsetDateTime.query(query);
System.out.println("zoneId: " + zoneId);
Output:

zonedDateTime: 2021-07-10T00:45:45.503914+06:00[Asia/Bishkek]
zoneId: Asia/Bishkek

offsetDateTime: 2021-07-09T20:45:45.506205+02:00
zoneId: +02:00
The reference of the static method ZoneId.from(TemporalAccessor) can be used as a TemporalQuery, which corresponds to this method, except it throws an exception if the result cannot be obtained.
TemporalQueries_zone_ex2.java

// Method referene
TemporalQuery<ZoneId> query = ZoneId::from; // Same as: TemporalQueries.zone()

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("zonedDateTime: " + zonedDateTime);

ZoneId zoneId = zonedDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("zoneId: " + zoneId);
System.out.println();
//
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println("offsetDateTime: " + offsetDateTime);

zoneId = offsetDateTime.query(query); // Throw exception if the result cannot be obtained.
System.out.println("zoneId: " + zoneId);
Output:

zonedDateTime: 2021-07-10T00:45:45.503914+06:00[Asia/Bishkek]
zoneId: Asia/Bishkek

offsetDateTime: 2021-07-09T20:45:45.506205+02:00
zoneId: +02:00

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.