Java LocalDateTime hashCode() Method

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

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.

Java
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)
CallWhat 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
// java.time.LocalDateTime
public int hashCode()

Parameters

ParameterDescription
NonehashCode() takes no parameters.

Return Value

Return ValueDescription
intA hash code that is the same for every date-time equal to this one.

Compare with Similar Methods

Related methods:

MethodHow 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

Example 1 — See the Equals and HashCode Rule

Compare the hash codes of two equal date-times, and check the rule for several pairs.

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

How It Works

  1. Two separate objects. a and b hold the same date and time, so equals prints true.
  2. Equal objects, equal hash codes. The second line prints true. This is the rule of the contract.
  3. Built in different ways. x is created directly and y is created by adding 45 seconds. They end up equal.
  4. 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.

Java
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

  1. Three adds. The first and the third date-time are equal.
  2. The set uses hashCode and equals. The third add finds an equal element and is ignored.
  3. Size 2. Only two different date-times remain.
  4. 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.

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

How It Works

  1. Two entries. The keys are date-times and the values are text.
  2. A new object finds the entry. lookup is not the object that was stored, but it is equal, and has the same hash code.
  3. A match prints the value. The first lookup prints "lunch".
  4. No match gives null. Nothing is stored at 14:00, so get returns null, which is printed as the word null.

Frequently Asked Questions

Yes. It is immutable and has proper equals() and hashCode() methods, so it is a safe key.
Not always. Equal objects must have equal hash codes, but different objects may share one. Use equals() to be sure.
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.
Hash-based collections first use the hash code to find a place, and then equals() to confirm. If they disagree, objects get lost.
Put them in a HashSet, or use new LinkedHashSet<>(list) to also keep the order.

Next: isAfter()

Check if this date-time is later than another.

isAfter() 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.

7 people found this page helpful