Java Programs›Date & Time›List All Dates Between Two Dates

List All Dates Between Two Dates in Java

beginner·  Date & Time  ·  Date & Time

Problem

A date range is every calendar day from a start date to an end date; plusDays moves across month and year boundaries automatically.

Print every date between two given dates, including both ends, with a short weekday name.

Input
2026-02-26 to 2026-03-03
Output
Thu 26 Feb ... Tue 3 Mar

Java Program

Java
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class ListDatesBetween { public static void main(String[] args) { LocalDate start = LocalDate.of(2026, 2, 26); LocalDate end = LocalDate.of(2026, 3, 3); DateTimeFormatter f = DateTimeFormatter.ofPattern("EEE d MMM", Locale.ENGLISH); for (LocalDate d = start; !d.isAfter(end); d = d.plusDays(1)) { // inclusive of both ends System.out.println(d.format(f)); } } }

Output

Thu 26 Feb Fri 27 Feb Sat 28 Feb Sun 1 Mar Mon 2 Mar Tue 3 Mar

Core Logic

A for loop over dates works like one over integers: start at the first date and add one day until you pass the end.

How It Works
  1. 1The loop starts at start and continues while the date isn't after end.
  2. 2d.plusDays(1) moves to the next day, handling February's end automatically.
  3. 3Each date is formatted with EEE d MMM and an English locale.
After Saturday 28 February 2026 comes Sunday 1 March, because 2026 isn't a leap year.
💡

Key Point: Using !d.isAfter(end) includes the end date; d.isBefore(end) would stop one day early.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: One iteration per day in the range.

Key Concepts

for loop over LocalDateisAfter()DateTimeFormatter

Approach 2: Using datesUntil (Java 9)

Java
import java.time.LocalDate; import java.time.Period; import java.util.List; import java.util.stream.Collectors; public class ListDatesBetweenStream { public static void main(String[] args) { LocalDate start = LocalDate.of(2026, 2, 26); LocalDate end = LocalDate.of(2026, 3, 3); List<LocalDate> days = start.datesUntil(end.plusDays(1)).collect(Collectors.toList()); // Java 9, end exclusive System.out.println(days); List<LocalDate> weekly = start.datesUntil(LocalDate.of(2026, 3, 31), Period.ofWeeks(1)) .collect(Collectors.toList()); System.out.println("Every week: " + weekly); } }

Output

[2026-02-26, 2026-02-27, 2026-02-28, 2026-03-01, 2026-03-02, 2026-03-03] Every week: [2026-02-26, 2026-03-05, 2026-03-12, 2026-03-19, 2026-03-26]

Core Logic

LocalDate.datesUntil returns the range as a stream, optionally stepping by a Period.

How It Works
  1. 1start.datesUntil(end.plusDays(1)) includes the end date, since the end is exclusive.
  2. 2The dates are collected into a list.
  3. 3datesUntil(limit, Period.ofWeeks(1)) produces one date per week.
The weekly list gives the five Thursdays from 26 February to 26 March.
💡

Key Point: Remember that datesUntil excludes its end date.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: All d dates are stored in the list.

Key Concepts

datesUntil()Period stepStream<LocalDate>

Interview Questions

How do you list only the weekdays in a range?

Filter the stream: datesUntil(end).filter(d -> d.getDayOfWeek().getValue() <= 5), since Monday to Friday are 1 to 5.

How many days are between the two dates?

ChronoUnit.DAYS.between(start, end) + 1 counts both ends.

Learn the Concept

Related Programs