在Java中,获取给定月份的所有周末日期

在Java中,获取给定月份的所有周末日期,第1张

在Java中,获取给定月份的所有周末日期

这是带有描述步骤注释的粗略版本:

// 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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5091072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存