Java LocalDateTime isBefore() Method

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

Definition

In Java, a.isBefore(b) returns true if a is earlier than b on the timeline, and false if it is later or exactly the same.

It is easier to read than compareTo(), and it is the usual way to ask "has this started yet?" or "is this still valid?".

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

Syntax

Call it on one date-time and pass the other.

Java
LocalDateTime cutoff = LocalDateTime.of(2026, 4, 1, 0, 0);
LocalDateTime purchase = LocalDateTime.of(2026, 3, 20, 10, 0);

boolean early = purchase.isBefore(cutoff);   // true
CallWhat it does
a.isBefore(b)True if a is earlier than b.
a.isAfter(b)True if a is later than b.
a.isEqual(b)True if they are at the same moment.
a.compareTo(b) < 0The same test written with compareTo().

a.isBefore(b) is the same as b.isAfter(a).

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

Parameters

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

Return Value

Return ValueDescription
booleantrue if this date-time is earlier than other, otherwise false.

Compare with Similar Methods

Ways to compare date-times:

MethodHow it differs
isAfter(other)The opposite question: is this later?
isEqual(other)Is this exactly the same moment?
compareTo(other)Gives a number for the order, which sorting uses.

Java Examples

Example 1 — Choose a Price Before a Cut-off

Tickets are cheaper before 1 April. Choose the price for two purchase times.

Java
import java.time.LocalDateTime;

public class LdtIsBeforePrice {
    public static void main(String[] args) {
        LocalDateTime cutoff = LocalDateTime.of(2026, 4, 1, 0, 0);
        LocalDateTime[] purchases = {
            LocalDateTime.of(2026, 3, 20, 10, 0),
            LocalDateTime.of(2026, 4, 1, 0, 0),
            LocalDateTime.of(2026, 4, 2, 9, 30)
        };

        for (LocalDateTime purchase : purchases) {
            int price = purchase.isBefore(cutoff) ? 40 : 60;
            System.out.println(purchase + " -> " + price);
        }
    }
}

How It Works

  1. A cut-off. Midnight at the start of 1 April 2026.
  2. 20 March is before the cut-off. isBefore is true, so the price is 40.
  3. Exactly at the cut-off. A date-time is not before itself, so the price is 60.
  4. 2 April is after. The price is 60 as well.

Example 2 — Test the Boundary Between Two Moments

Compare the last minute of a day with midnight, midnight with itself, and the same moment as seen by !isAfter.

Java
import java.time.LocalDateTime;

public class LdtIsBeforeBoundary {
    public static void main(String[] args) {
        LocalDateTime lastMinute = LocalDateTime.of(2026, 3, 14, 23, 59);
        LocalDateTime midnight = LocalDateTime.of(2026, 3, 15, 0, 0);

        System.out.println("23:59 before midnight:   " + lastMinute.isBefore(midnight));
        System.out.println("midnight before itself:  " + midnight.isBefore(midnight));
        System.out.println("midnight or earlier:     " + !midnight.isAfter(midnight));
    }
}

How It Works

  1. One minute earlier. 23:59 on the 14th is before midnight on the 15th, so the answer is true.
  2. The same moment. Midnight is not before itself, so the second line is false.
  3. Earlier or the same. !midnight.isAfter(midnight) is true, so this is the way to include the boundary.

Example 3 — Find the Earliest Date-Time by Hand

Loop through a list and keep the earliest one found so far, using isBefore.

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

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

        LocalDateTime earliest = logs.get(0);
        for (LocalDateTime log : logs) {
            if (log.isBefore(earliest)) {
                earliest = log;
            }
        }
        System.out.println("earliest: " + earliest);
    }
}

How It Works

  1. Start with the first. earliest begins as the first date-time in the list.
  2. Test each one. If a date-time is before the current earliest, it takes its place.
  3. Only one is earlier. The second entry, 23:40 on 14 March, is before 14:05 on the 15th.
  4. The result. The loop ends with 14 March at 23:40.

Frequently Asked Questions

Use a.isBefore(b). It returns true only if a is earlier than b.
Use dt.isBefore(LocalDateTime.now()).
No. Equal date-times are neither before nor after each other. Use !a.isAfter(b) for "earlier or the same".
They give the same answer. isBefore() is easier to read.
Use !dt.isBefore(start) && dt.isBefore(end) for a range that includes the start and excludes the end.

Next: isEqual()

Check if two date-times are at the same moment on the local timeline.

isEqual() 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