Date Range Overlap Check
Check whether two inclusive date ranges overlap with a two-comparison rule covering containment, adjacency, and disjoint spans.
Code
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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.
FAQ
- How do you run Date Range Overlap Check?
- The file is self-contained: run it directly, no dependencies to install beforehand.
Report a problem with this exampleOpens a pre-filled issue on GitHub.