HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – TemporalQuery

Java 8 – TemporalQuery

TemporalQuery is a standard way of querying the temporal objects for making better business decisions. In Java 8, all major date time classes implement Temporal and TemporalAccessor interfaces so TemporalQuery can be run against all those Java classes.

1. TemporalQuery interface

TemporalQuery is a functional interface and can, therefore, be used as the assignment target for a lambda expression or method reference. The method queryForm() takes the temporal object to query and return the queried value.

The implementation defines the logic of the query and is responsible for documenting that logic. It may use any method on TemporalAccessor to determine the result.

The input object must not be altered.

@FunctionalInterface
public interface TemporalQuery<R> 
{
	R queryFrom(TemporalAccessor temporal);
}

2. TemporalQuery Example

Let’s see a few examples to understand this interface better.

2.1. A given tome is within business hours?

We can use TemporalQuery to determine if any given time is within a certain range. E.g. time lies between business hours or not.

LocalTime now = LocalTime.now();

System.out.println("Currently Working :: " + now.query(WorkingHoursQuery));

private static final TemporalQuery<Boolean> WorkingHoursQuery = temporal -> {
	LocalTime t = LocalTime.from(temporal);
	return t.compareTo(LocalTime.of(9, 0)) >= 0 
			&& t.compareTo(LocalTime.of(17, 0)) < 0;
};

Program output:

Currently Working :: false

2.2. Get current financial quarter

We can also use TemporalQuery to determine the current financial quarter of the year. In the below example, the first financial quarter is considered from January to March. Change the method implementation for desired behavior.

LocalDate today = LocalDate.now();

System.out.println("Current Financial Quarter :: " + today.query(CurrentQuarterQuery));

private static final TemporalQuery<Integer> CurrentQuarterQuery = temporal -> {
	LocalDate date = LocalDate.from(temporal);
	return (date.getMonthValue() / 3) + 1;
};

Program output:

Current Financial Quarter :: 2

Drop me your questions related to using TemporalQuery with dates in Java 8.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces