In Java, a.equals(b) returns true if b is a LocalDateTime with exactly the same date and the same time as a, down to the last nanosecond.
Always use equals(), and not ==, to compare date-times. The operator == only checks whether two variables point to the same object.
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 overrides Object.equals().
Syntax
Call it on one date-time and pass the other.
Java
LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime b = LocalDateTime.of(2026, 3, 15, 10, 30);
boolean same = a.equals(b); // true
boolean sameObject = (a == b); // false: two different objects
Call
What it does
a.equals(b)
True if the date and the time are exactly the same.
a.isEqual(b)
Compares the position on the local timeline. For a LocalDateTime it gives the same answer.
a.compareTo(b) == 0
Another way to test for equality.
Objects.equals(a, b)
A null-safe version, which is also true when both are null.
equals() is also what HashSet, HashMap, and List.contains() use.
Library declarations
Java
// java.time.LocalDateTime
public boolean equals(Object obj)
Parameters
Parameter
Description
obj (Object)
The object to compare with. It can be anything, even null.
Return Value
Return Value
Description
boolean
true only if obj is a LocalDateTime with the same date and time.
explicit zeros: true
one nano later: false
printed as: 2026-03-15T10:30:00.000000001
How It Works
Zero seconds, written two ways. The short form and the long form with zeros describe the same moment, so they are equal.
One nanosecond more.oneNano is one billionth of a second later, which is enough to make the result false.
The text shows it. The last line prints all nine digits of the fraction, so you can see what is different.
Example 3 — Compare with Other Types
Compare a date-time with its date, with its text, and with itself.
Java
import java.time.LocalDateTime;
public class LdtEqualsTypes {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("with its LocalDate: " + dt.equals(dt.toLocalDate()));
System.out.println("with its text: " + dt.equals("2026-03-15T10:30"));
System.out.println("with itself: " + dt.equals(dt));
System.out.println("same text: " + dt.toString().equals("2026-03-15T10:30"));
}
}
Output
with its LocalDate: false
with its text: false
with itself: true
same text: true
How It Works
A LocalDate is a different type. Even though it shows the same day, it has no time, so the result is false.
A String is a different type too. The text looks like the date-time, but equals only accepts another LocalDateTime.
An object equals itself. The third line prints true.
Compare texts if you need to. Convert to a String first, as in the last line, and then compare the two strings.
Frequently Asked Questions
Use a.equals(b). Do not use ==, which compares the objects and not the values.
The nanoseconds may differ, or the other object is of a different type such as a LocalDate or ZonedDateTime.
For two LocalDateTime objects they give the same answer. isEqual also works with date-times of other calendars, and for ZonedDateTime it compares the moment in time, not the zone.
No, equals() returns false for a different type. Convert one of them first, for example with toLocalDateTime().
Cut both with truncatedTo(ChronoUnit.MINUTES) first, and then call equals().