В Java получите все выходные даты в данном месяце

Я должен найти все выходные даты за данный месяц и данный год.

Например: Для 01 (месяц), 2010 (год), вывод должен быть: 2,3,9,10,16,17,23,24,30,31, все выходные даты.

13
задан Ortomala Lokni 18 May 2016 в 21:48
поделиться

1 ответ

Вот грубая версия с комментариями, описывающими шаги:

// create a Calendar for the 1st of the required month
int year = 2010;
int month = Calendar.JANUARY;
Calendar cal = new GregorianCalendar(year, month, 1);
do {
    // get the day of the week for the current day
    int day = cal.get(Calendar.DAY_OF_WEEK);
    // check if it is a Saturday or Sunday
    if (day == Calendar.SATURDAY || day == Calendar.SUNDAY) {
        // print the day - but you could add them to a list or whatever
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
    }
    // advance to the next day
    cal.add(Calendar.DAY_OF_YEAR, 1);
}  while (cal.get(Calendar.MONTH) == month);
// stop when we reach the start of the next month
22
ответ дан 1 December 2019 в 19:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: