Java LocalDateTime compareTo() Method

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

Definition

In Java, a.compareTo(b) compares two LocalDateTime objects. The result is negative if a is earlier than b, zero if they are the same, and positive if a is later.

Because LocalDateTime implements Comparable, this is the method that sorting and sorted collections use behind the scenes. It compares the date first, and then the time of day.

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 Comparable and ChronoLocalDateTime, which LocalDateTime implements.

Syntax

Call it on one date-time and pass the other. Only look at whether the answer is below, equal to, or above zero.

Java
LocalDateTime early = LocalDateTime.of(2026, 3, 15, 9, 0);
LocalDateTime late = LocalDateTime.of(2026, 3, 15, 17, 30);

int result = early.compareTo(late);   // negative: early comes first
boolean isEarlier = result < 0;       // true
CallWhat it does
a.compareTo(b)Negative, zero, or positive, depending on the order of a and b.
a.isBefore(b)A clearer way to ask "is a earlier than b?".
a.isAfter(b)A clearer way to ask "is a later than b?".
Collections.sort(list)Sorts a list of date-times, using compareTo().

Do not test the result against -1 or 1. Only the sign is promised.

Library declarations
Java
// java.time.LocalDateTime
public int compareTo(ChronoLocalDateTime<?> other)

Parameters

ParameterDescription
other (ChronoLocalDateTime<?>)The date-time to compare with. It must not be null.

Return Value

Return ValueDescription
intA negative number if this is earlier, zero if equal, a positive number if this is later.

Compare with Similar Methods

Ways to compare date-times:

MethodHow it differs
isBefore(other)Returns true if this is earlier. Easier to read for a yes/no question.
isAfter(other)Returns true if this is later.
equals(obj)Checks if two date-times are exactly the same.

Java Examples

Example 1 — Find Which Date-Time Comes First

Compare an early and a late time, in both directions, and an equal pair. Print only the sign.

Java
import java.time.LocalDateTime;

public class LdtCompareToDemo {
    public static void main(String[] args) {
        LocalDateTime early = LocalDateTime.of(2026, 3, 15, 9, 0);
        LocalDateTime late = LocalDateTime.of(2026, 3, 15, 17, 30);
        LocalDateTime sameAsEarly = LocalDateTime.of(2026, 3, 15, 9, 0);

        System.out.println("early vs late:  " + Integer.signum(early.compareTo(late)));
        System.out.println("late vs early:  " + Integer.signum(late.compareTo(early)));
        System.out.println("early vs same:  " + Integer.signum(early.compareTo(sameAsEarly)));
    }
}

How It Works

  1. Three date-times. Two are on the same day at different times, and the third equals the first.
  2. Early against late. The early time comes first, so the result is negative.
  3. Late against early. The result is positive.
  4. Equal times. The result is zero. Integer.signum turns any answer into -1, 0, or 1, so the output does not depend on the exact number.

Example 2 — Sort a List of Date-Times

Put four events in a list in the wrong order and sort them. The sort uses compareTo.

Java
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LdtCompareToSort {
    public static void main(String[] args) {
        List<LocalDateTime> events = new ArrayList<>();
        events.add(LocalDateTime.of(2026, 5, 1, 9, 0));
        events.add(LocalDateTime.of(2026, 3, 15, 18, 30));
        events.add(LocalDateTime.of(2026, 3, 15, 8, 0));
        events.add(LocalDateTime.of(2025, 12, 31, 23, 59));

        Collections.sort(events);

        for (LocalDateTime event : events) {
            System.out.println(event);
        }
    }
}

How It Works

  1. An unsorted list. The events are added in a random order.
  2. Sort uses compareTo. Collections.sort(events) needs no extra code, because LocalDateTime already knows its natural order.
  3. The date counts first. The December 2025 event comes first, and May 2026 comes last.
  4. Then the time. The two events on 15 March are ordered by their time, 08:00 before 18:30.

Example 3 — Find the Earliest and the Latest

Use Collections.min and Collections.max, which also rely on compareTo.

Java
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LdtCompareToMinMax {
    public static void main(String[] args) {
        List<LocalDateTime> logins = Arrays.asList(
            LocalDateTime.of(2026, 3, 15, 14, 5),
            LocalDateTime.of(2026, 3, 14, 23, 40),
            LocalDateTime.of(2026, 3, 16, 7, 15)
        );

        System.out.println("first login: " + Collections.min(logins));
        System.out.println("last login:  " + Collections.max(logins));
    }
}

How It Works

  1. Three logins. They are on three different days.
  2. min finds the smallest. The smallest date-time is the earliest, 14 March at 23:40.
  3. max finds the largest. The largest is the latest, 16 March at 07:15.
  4. No loop needed. Both methods use compareTo() for you.

Frequently Asked Questions

Use isBefore(), isAfter(), or equals() for yes/no answers, or compareTo() to get a negative, zero, or positive number.
A negative number if the first is earlier, zero if they are equal, and a positive number if it is later. Do not rely on the exact value.
Call Collections.sort(list) or list.sort(null). LocalDateTime is Comparable, so no comparator is needed.
Use Collections.sort(list, Collections.reverseOrder()), or list.sort(Comparator.reverseOrder()).
compareTo(null) throws a NullPointerException. Check for null first.

Next: equals()

Check if two objects are the same date and time.

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

9 people found this page helpful