在Java中,获取给定月份的所有周末日期
我需要找到给定月份和年份的所有周末日期。
例如:对于01(月),2010(年),输出应为:所有周末日期:2,3,9,10,16,17,23,24,30,31。
回答:
这是带有描述步骤的注释的粗略版本:
// create a Calendar for the 1st of the required monthint 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
以上是 在Java中,获取给定月份的所有周末日期 的全部内容, 来源链接: utcz.com/qa/428743.html