Java LocalDateTime query() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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");
CallWhat 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
// java.time.LocalDateTime
public <R> R query(TemporalQuery<R> query)

Parameters

ParameterDescription
query (TemporalQuery<R>)The query to run, such as TemporalQueries.localDate(), or a lambda that takes a TemporalAccessor.

Return Value

Return ValueDescription
RThe result of the query. Its type is whatever the query returns.

Compare with Similar Methods

Ways to take part of a date-time:

MethodHow it differs
toLocalDate()The direct way to get the date part.
toLocalTime()The direct way to get the time part.
get(field)Reads a single field as a number.

Java Examples

Example 1 — Use the Built-In Queries

Ask for the date part, the time part, the precision, the chronology, and the zone.

Java
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()));
    }
}

How It Works

  1. Ready-made queries. TemporalQueries has one method for each common question.
  2. Date and time. localDate() and localTime() give the two parts, each as its own type.
  3. Precision and calendar. The smallest unit is nanoseconds, and the calendar system is ISO.
  4. 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.

Java
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));
    }
}

How It Works

  1. A query is a one-method interface. It takes a TemporalAccessor and returns a value, so a lambda fits.
  2. The rule. The lambda reads the hour with get(ChronoField.HOUR_OF_DAY) and picks a word.
  3. Run it with query(). The same object is used for both date-times, and the type of the result is String.
  4. 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.

Java
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));
    }
}

How It Works

  1. A block lambda. The rule needs two lines, so it uses braces and a return.
  2. DayOfWeek.from(t). It extracts the weekday from any object that has a date.
  3. Store the rule in a variable. The variable isWeekend can be used for any date-time, and also for a LocalDate or a ZonedDateTime.
  4. The results. 14 March 2026 is a Saturday, so the answer is true, and Monday gives false.

Frequently Asked Questions

It is an interface with one method that takes a date-time object and returns a result. You pass it to query().
Use dt.toLocalDate(). dt.query(TemporalQueries.localDate()) does the same.
A LocalDateTime has no time zone, so there is nothing to return. Queries return null when the thing they look for is missing.
Yes. TemporalQuery has one abstract method, so a lambda such as t -> t.get(ChronoField.HOUR_OF_DAY) works.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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