You can do it as follows:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getWeeklyDateByStringofDays("Mon, Tue, Wed", LocalDate.parse("2020-09-14"),
LocalDate.parse("2020-12-14")));
}
static List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, LocalDate endDate) {
// Split the string on optional whitespace followed by comma which in turn may
// be followed by optional whitespace
String[] daysOfWeekList = daysOfWeek.split("\\s*,\\s*");
// The list to be populated with desired dates and returned
List<LocalDate> result = new ArrayList<>();
// Formatter to get only day name e.g. Mon from the date
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);
for (String day : daysOfWeekList) {
// Loop starting with the startDate until the endDate with a step of one day
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
if (date.format(dtf).equals(day)) {
result.add(date);
}
}
}
// Sort the list
Collections.sort(result);
return result;
}
}
Output:
[2020-09-14, 2020-09-15, 2020-09-16, 2020-09-21, 2020-09-22, 2020-09-23, 2020-09-28, 2020-09-29, 2020-09-30, 2020-10-05, 2020-10-06, 2020-10-07, 2020-10-12, 2020-10-13, 2020-10-14, 2020-10-19, 2020-10-20, 2020-10-21, 2020-10-26, 2020-10-27, 2020-10-28, 2020-11-02, 2020-11-03, 2020-11-04, 2020-11-09, 2020-11-10, 2020-11-11, 2020-11-16, 2020-11-17, 2020-11-18, 2020-11-23, 2020-11-24, 2020-11-25, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-14]
Note: If the day names in the parameter can be in any case, replace date.format(dtf).equals(day)
with date.format(dtf).equalsIgnoreCase(day)
in the code given above.