Java 日历:我想从日历中删除特定的星期几

Java Calendar: I want to remove specific Day of week from calendar

更多详细信息我想去掉一周中的特定几天。

我的意思是:{Sat(1), Sun(2), Mon(3) , Tues(4) , Wed (5),Thurs(6) ,Fri(7)}.

删除工作日:{Sat(1), Sun(2), Mon(3) , Tues(4) , Wed (5)}

所以我可以得到从特定开始日期到现在的日期没有排除天数的天数。

提示:我必须使用Java日历

问题是动态指定日期

如果可以使用 LocalDate 和流,则可以应用函数式方法。

LocalDate start = LocalDate.of(2015, 2, 1);
LocalDate end = LocalDate.of(2015, 2, 28);

List<DayOfWeek> includedDays = Arrays.asList(DayOfWeek.THURSDAY, DayOfWeek.FRIDAY);

long daysCount = Stream.iterate(start, date -> date.plusDays(1))
        .limit(ChronoUnit.DAYS.between(start, end))
        .filter(d -> includedDays.contains(d.getDayOfWeek()))
        .count();

如果您坚持使用日历,只需使用:

GregorianCalendar calendarStart = new GregorianCalendar(2015, 2, 1);
GregorianCalendar calendarEnd = new GregorianCalendar(2015, 2, 28);

List<Integer> includedDays = Arrays.asList(GregorianCalendar.THURSDAY, GregorianCalendar.FRIDAY);
long count = 0;
while(!calendarStart.equals(calendarEnd)) {
    if(includedDays.contains(calendarStart.get(Calendar.DAY_OF_WEEK))) {
        count ++;
    }
    calendarStart.add(Calendar.DATE, 1);
}