List All Dates Between Two Dates in Java
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.
Java Program
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
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.
- 1The loop starts at
startand continues while the date isn't afterend. - 2
d.plusDays(1)moves to the next day, handling February's end automatically. - 3Each date is formatted with
EEE d MMMand an English locale.
Key Point: Using !d.isAfter(end) includes the end date; d.isBefore(end) would stop one day early.
Why: One iteration per day in the range.
Key Concepts
Approach 2: Using datesUntil (Java 9)
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
Core Logic
LocalDate.datesUntil returns the range as a stream, optionally stepping by a Period.
- 1
start.datesUntil(end.plusDays(1))includes the end date, since the end is exclusive. - 2The dates are collected into a list.
- 3
datesUntil(limit, Period.ofWeeks(1))produces one date per week.
Key Point: Remember that datesUntil excludes its end date.
Why: All d dates are stored in the list.
Key Concepts
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.