Home Java LocalDateTime Methods query() Java LocalDateTime query() Method Definition In Java, query(q) hands the date-time to a TemporalQuery and returns whatever that query computes. The result can be of any type: a LocalDate, a String, a boolean, and so on.
The class TemporalQueries has ready-made queries. You can also write your own as a short lambda, which is a nice way to put a rule such as "is this a weekend?" in one place.
An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file. It comes from TemporalAccessor, which LocalDateTime implements.
Syntax Pass a built-in query or your own lambda.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);
LocalDate date = dt.query(TemporalQueries.localDate()); // 2026-03-15
LocalTime time = dt.query(TemporalQueries.localTime()); // 14:30
String part = dt.query(t -> t.get(ChronoField.HOUR_OF_DAY) < 12 ? "morning" : "later"); Call What it does dt.query(TemporalQueries.localDate())Returns the date part as a LocalDate. dt.query(TemporalQueries.localTime())Returns the time part as a LocalTime. dt.query(TemporalQueries.precision())Returns the smallest unit the date-time can hold. dt.query(t -> ...)Runs your own rule on the date-time.
query() is how toLocalDate() and toLocalTime() work behind the scenes. For those simple cases, call the direct methods.
Library declarations // java.time.LocalDateTime
public <R> R query(TemporalQuery<R> query) Parameters Parameter Description query (TemporalQuery<R>)The query to run, such as TemporalQueries.localDate(), or a lambda that takes a TemporalAccessor.
Return Value Return Value Description RThe result of the query. Its type is whatever the query returns.
Important
Queries that look for something the date-time does not have return null, not an exception. For a LocalDateTime, TemporalQueries.zone() returns null because there is no zone. Check for null before you use such a result. Inside your own lambda, read values with get(ChronoField.X).
Compare with Similar Methods Ways to take part of a date-time:
Java Examples 1. Built-in queries 2. Your own query 3. Reusable rule Example 1 — Use the Built-In Queries Ask for the date part, the time part, the precision, the chronology, and the zone.
import java.time.LocalDateTime;
import java.time.temporal.TemporalQueries;
public class LdtQueryBuiltin {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);
System.out.println("date: " + dt.query(TemporalQueries.localDate()));
System.out.println("time: " + dt.query(TemporalQueries.localTime()));
System.out.println("precision: " + dt.query(TemporalQueries.precision()));
System.out.println("chronology: " + dt.query(TemporalQueries.chronology()));
System.out.println("zone: " + dt.query(TemporalQueries.zone()));
}
} date: 2026-03-15
time: 14:30
precision: Nanos
chronology: ISO
zone: null How It Works Ready-made queries. TemporalQueries has one method for each common question.Date and time. localDate() and localTime() give the two parts, each as its own type.Precision and calendar. The smallest unit is nanoseconds, and the calendar system is ISO.No zone. A LocalDateTime has none, so the zone query returns null, which is printed as the word null.Example 2 — Write Your Own Query Write a lambda that says "morning" or "later" and run it on two date-times.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQuery;
public class LdtQueryLambda {
public static void main(String[] args) {
TemporalQuery<String> partOfDay = t -> t.get(ChronoField.HOUR_OF_DAY) < 12 ? "morning" : "later";
LocalDateTime early = LocalDateTime.of(2026, 3, 15, 8, 15);
LocalDateTime late = LocalDateTime.of(2026, 3, 15, 19, 45);
System.out.println(early + " -> " + early.query(partOfDay));
System.out.println(late + " -> " + late.query(partOfDay));
}
} 2026-03-15T08:15 -> morning
2026-03-15T19:45 -> later How It Works A query is a one-method interface. It takes a TemporalAccessor and returns a value, so a lambda fits.The rule. The lambda reads the hour with get(ChronoField.HOUR_OF_DAY) and picks a word.Run it with query(). The same object is used for both date-times, and the type of the result is String.The results. 08:15 is "morning" and 19:45 is "later".Example 3 — Keep a Rule in One Place Write a weekend rule once, store it in a variable, and use it on a Saturday and a Monday.
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalQuery;
public class LdtQueryWeekend {
public static void main(String[] args) {
TemporalQuery<Boolean> isWeekend = t -> {
DayOfWeek day = DayOfWeek.from(t);
return day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY;
};
LocalDateTime saturday = LocalDateTime.of(2026, 3, 14, 10, 0);
LocalDateTime monday = LocalDateTime.of(2026, 3, 16, 10, 0);
System.out.println("Saturday: " + saturday.query(isWeekend));
System.out.println("Monday: " + monday.query(isWeekend));
}
} Saturday: true
Monday: false How It Works A block lambda. The rule needs two lines, so it uses braces and a return.DayOfWeek.from(t). It extracts the weekday from any object that has a date.Store the rule in a variable. The variable isWeekend can be used for any date-time, and also for a LocalDate or a ZonedDateTime.The results. 14 March 2026 is a Saturday, so the answer is true, and Monday gives false.Frequently Asked Questions What is a TemporalQuery in Java? It is an interface with one method that takes a date-time object and returns a result. You pass it to query().
How do I get the LocalDate from a LocalDateTime? Use dt.toLocalDate(). dt.query(TemporalQueries.localDate()) does the same.
Why does the zone query return null? A LocalDateTime has no time zone, so there is nothing to return. Queries return null when the thing they look for is missing.
Can I use a lambda with query()? Yes. TemporalQuery has one abstract method, so a lambda such as t -> t.get(ChronoField.HOUR_OF_DAY) works.
What is the difference between query() and get()? get() reads one field as a number. query() can compute any result of any type from the whole date-time.
Next: range() Get the valid range of values of a field for this date-time.
range() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion