Code Example:
package org.code2care.examples;
import java.time.LocalDate;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateDateBetweenDays {
public static void main(String[] args) {
List dates = List.of(
LocalDate.of(2023, 10, 10),
LocalDate.of(2023, 10, 11),
LocalDate.of(2023, 10, 12),
LocalDate.of(2023, 10, 13),
LocalDate.of(2023, 10, 14),
LocalDate.of(2023, 10, 15)
);
LocalDate startDate = LocalDate.of(2023, 10, 10);
LocalDate endDate = LocalDate.of(2023, 10, 14);
Predicate isDateInRange = date -> date.isAfter(startDate) && date.isBefore(endDate);
List filteredDates = dates.stream()
.filter(isDateInRange)
.toList();
System.out.println("Dates between " + startDate + " and " + endDate + ": " + filteredDates);
}
}
Output:
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!