Home Java LocalDateTime Methods isBefore() Java LocalDateTime isBefore() Method 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.
LocalDateTime cutoff = LocalDateTime.of(2026, 4, 1, 0, 0);
LocalDateTime purchase = LocalDateTime.of(2026, 3, 20, 10, 0);
boolean early = purchase.isBefore(cutoff); // true Call What 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.time.LocalDateTime
public boolean isBefore(ChronoLocalDateTime<?> other) Parameters Parameter Description other (ChronoLocalDateTime<?>)The date-time to compare with. It must not be null.
Return Value Return Value Description booleantrue if this date-time is earlier than other, otherwise false.
Important
Equal date-times are not before each other, so a.isBefore(a) is false. If you want "earlier or the same", write !a.isAfter(b). This matters at boundaries such as "offer valid until midnight". Passing null throws a NullPointerException.
Compare with Similar Methods Ways to compare date-times:
Java Examples 1. Early bird price 2. Boundaries 3. Find the earliest Example 1 — Choose a Price Before a Cut-off Tickets are cheaper before 1 April. Choose the price for two purchase times.
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);
}
}
} 2026-03-20T10:00 -> 40
2026-04-01T00:00 -> 60
2026-04-02T09:30 -> 60 How It Works A cut-off. Midnight at the start of 1 April 2026.20 March is before the cut-off. isBefore is true, so the price is 40.Exactly at the cut-off. A date-time is not before itself, so the price is 60.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.
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));
}
} 23:59 before midnight: true
midnight before itself: false
midnight or earlier: true How It Works One minute earlier. 23:59 on the 14th is before midnight on the 15th, so the answer is true.The same moment. Midnight is not before itself, so the second line is false.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.
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);
}
} earliest: 2026-03-14T23:40 How It Works Start with the first. earliest begins as the first date-time in the list.Test each one. If a date-time is before the current earliest, it takes its place.Only one is earlier. The second entry, 23:40 on 14 March, is before 14:05 on the 15th.The result. The loop ends with 14 March at 23:40.Frequently Asked Questions How do I check if a date is before another date in Java? Use a.isBefore(b). It returns true only if a is earlier than b.
How do I check if a LocalDateTime is in the past? Use dt.isBefore(LocalDateTime.now()).
Is isBefore() true for equal date-times? No. Equal date-times are neither before nor after each other. Use !a.isAfter(b) for "earlier or the same".
What is the difference between isBefore() and compareTo() < 0? They give the same answer. isBefore() is easier to read.
How do I check if a date-time is between two others? 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 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.
9 people found this page helpful
Helpful Share Copy link Suggestion