Date Range Overlap Check

Check whether two inclusive date ranges overlap with a two-comparison rule covering containment, adjacency, and disjoint spans.

JavaForm Validation

Code

snippet
import java.time.LocalDate;

public class DateRangeOverlap {

    // date range overlap: two closed ranges intersect iff neither starts after the other ends
    static boolean overlaps(DateRange a, DateRange b) {
        return !a.start().isAfter(b.end()) && !b.start().isAfter(a.end());
    }

    record DateRange(LocalDate start, LocalDate end) {}

    public static void main(String[] args) {
        DateRange booking = new DateRange(LocalDate.parse("2026-12-01"), LocalDate.parse("2026-12-10"));
        record Probe(String name, DateRange span) {}
        Probe[] probes = {
                new Probe("overlaps end", new DateRange(LocalDate.parse("2026-12-08"), LocalDate.parse("2026-12-15"))),
                new Probe("fully inside", new DateRange(LocalDate.parse("2026-12-03"), LocalDate.parse("2026-12-05"))),
                new Probe("before", new DateRange(LocalDate.parse("2026-11-20"), LocalDate.parse("2026-11-30"))),
                new Probe("starts on last day", new DateRange(LocalDate.parse("2026-12-10"), LocalDate.parse("2026-12-20"))),
                new Probe("starts day after", new DateRange(LocalDate.parse("2026-12-11"), LocalDate.parse("2026-12-20")))};
        for (Probe probe : probes) {
            System.out.println(probe.name() + ": " + overlaps(booking, probe.span()));
        }
    }
}

How it works

Everything happens inside the classes DateRangeOverlap, DateRange and Probe and the functions overlaps and main.

One loop does the cycling here: for (Probe probe : probes).

Points of care

  • It runs on exactly what the toolchain already ships — nothing extra.

Putting it to use

Entry signature: static boolean overlaps(DateRange a, DateRange b)

main is the driver: it invokes overlaps before returning.

The core lines of the flow

return !a.start().isAfter(b.end()) && !b.start().isAfter(a.end());

Interactive Tools for This Topic

FAQ

How do you run Date Range Overlap Check?
The file is self-contained: run it directly, no dependencies to install beforehand.