Home Java LocalDateTime Methods hashCode() Java LocalDateTime hashCode() Method Definition In Java, hashCode() returns an int that is worked out from the date and the time of a LocalDateTime. Two date-times that are equal always have the same hash code.
You rarely call it yourself. It is used by HashSet, HashMap, and similar collections to find objects quickly, which lets you use a LocalDateTime as a key.
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.hashCode().
Syntax Call it on a date-time. Compare hash codes of two equal objects to see the rule at work.
LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime b = LocalDateTime.of(2026, 3, 15, 10, 30);
boolean sameHash = a.hashCode() == b.hashCode(); // true, because a.equals(b) Call What it does dt.hashCode()The hash code as an int. a.equals(b)If this is true, the two hash codes are equal. Objects.hash(dt, other)Builds one hash code from several values. new HashSet<>(list)Uses hashCode() and equals() to remove duplicates.
Two different date-times can in rare cases have the same hash code. That is allowed, and the collections handle it using equals().
Library declarations // java.time.LocalDateTime
public int hashCode() Parameters Parameter Description None hashCode() takes no parameters.
Return Value Return Value Description intA hash code that is the same for every date-time equal to this one.
Important
The rule is: if a.equals(b), then a.hashCode() == b.hashCode() . The opposite is not promised. Do not print or store the number and expect it to stay the same in a different Java version: it is meant only for in-memory collections. Because a LocalDateTime is immutable, it is a safe key, since its hash code can never change.
Compare with Similar Methods Related methods:
Method How it differs equals(obj)The method that hashCode() must agree with. compareTo(other)Gives an order. It is used by sorted collections such as TreeSet. isEqual(other)Checks the timeline position without any hash.
Java Examples 1. Equal means same hash 2. Remove duplicates 3. Use as a map key Example 1 — See the Equals and HashCode Rule Compare the hash codes of two equal date-times, and check the rule for several pairs.
import java.time.LocalDateTime;
public class LdtHashCodeDemo {
public static void main(String[] args) {
LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime b = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("a equals b: " + a.equals(b));
System.out.println("same hash code: " + (a.hashCode() == b.hashCode()));
LocalDateTime x = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
LocalDateTime y = LocalDateTime.of(2026, 3, 15, 10, 30).plusSeconds(45);
System.out.println("x equals y: " + x.equals(y));
System.out.println("same hash code too: " + (x.hashCode() == y.hashCode()));
}
} a equals b: true
same hash code: true
x equals y: true
same hash code too: true How It Works Two separate objects. a and b hold the same date and time, so equals prints true.Equal objects, equal hash codes. The second line prints true. This is the rule of the contract.Built in different ways. x is created directly and y is created by adding 45 seconds. They end up equal.The rule still holds. Their hash codes are equal as well, so the last two lines both print true.Example 2 — Remove Duplicates with a HashSet Add three date-times to a HashSet, two of which are equal, and check the size.
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
public class LdtHashCodeSet {
public static void main(String[] args) {
Set<LocalDateTime> slots = new HashSet<>();
slots.add(LocalDateTime.of(2026, 3, 15, 10, 30));
slots.add(LocalDateTime.of(2026, 3, 15, 14, 0));
slots.add(LocalDateTime.of(2026, 3, 15, 10, 30));
System.out.println("size: " + slots.size());
System.out.println("has 14:00: " + slots.contains(LocalDateTime.of(2026, 3, 15, 14, 0)));
}
} How It Works Three adds. The first and the third date-time are equal.The set uses hashCode and equals. The third add finds an equal element and is ignored.Size 2. Only two different date-times remain.contains() works with a new object. The date-time passed to contains is a new object, but it equals one in the set, so the answer is true.Example 3 — Use a Date-Time as a Map Key Store appointments by date-time and look one up with a new object.
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class LdtHashCodeMap {
public static void main(String[] args) {
Map<LocalDateTime, String> appointments = new HashMap<>();
appointments.put(LocalDateTime.of(2026, 3, 15, 9, 0), "dentist");
appointments.put(LocalDateTime.of(2026, 3, 15, 13, 30), "lunch");
LocalDateTime lookup = LocalDateTime.of(2026, 3, 15, 13, 30);
System.out.println("at 13:30: " + appointments.get(lookup));
System.out.println("at 14:00: " + appointments.get(LocalDateTime.of(2026, 3, 15, 14, 0)));
}
} at 13:30: lunch
at 14:00: null How It Works Two entries. The keys are date-times and the values are text.A new object finds the entry. lookup is not the object that was stored, but it is equal, and has the same hash code.A match prints the value. The first lookup prints "lunch".No match gives null. Nothing is stored at 14:00, so get returns null, which is printed as the word null.Frequently Asked Questions Can I use LocalDateTime as a key in a HashMap? Yes. It is immutable and has proper equals() and hashCode() methods, so it is a safe key.
If two LocalDateTime objects have the same hash code, are they equal? Not always. Equal objects must have equal hash codes, but different objects may share one. Use equals() to be sure.
What is the hash code of a LocalDateTime? It is an int worked out from the date and the time. Its exact value is not important, and it can change between Java versions.
Why do equals() and hashCode() go together? Hash-based collections first use the hash code to find a place, and then equals() to confirm. If they disagree, objects get lost.
How do I remove duplicate date-times from a list? Put them in a HashSet, or use new LinkedHashSet<>(list) to also keep the order.
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.
7 people found this page helpful
Helpful Share Copy link Suggestion