排除打印循环的日期 [英] Excluding Dates from Print Loop

查看:67
本文介绍了排除打印循环的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何排除星期五和本月十三日的打印输出的任何一天。我正在尝试写一些以下的东西:if(dayofweek!= 5&& dayofmonth!= 13),然后打印。如何将其实现到以下代码中?

How would I go about excluding any day from my print outs that was a friday and the 13th day of the month. I'm attempting to write something along the lines of: if (dayofweek != 5 && dayofmonth != 13), then print. How could I implement that into the following code?

public class LoopDate {

public static void main(String[] args) {

    //Denotes that Tuesday is the first day of 2013
    int startingDayOfWeek = 2;
    int year = 2013;
    int numDays = 0;
    for (int month = 1; month <= 12; month++) {
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            numDays = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            numDays = 30;
            break;
        case 2:
            if (((year % 4 == 0) && !(year % 100 == 0))
                    || (year % 400 == 0))
                numDays = 29;
            else
                numDays = 28;
            break;
        default:
            System.out.println("Invalid month.");
            break;
        }
        for (int start = 1; start <= numDays; start++)
            System.out.println(month + "/" + start);


        }
    }
}


推荐答案

尝试

    GregorianCalendar c = new GregorianCalendar(2013, 0, 1);
    while (c.get(Calendar.YEAR) == 2013) {
        if (!(c.get(Calendar.DAY_OF_MONTH) == 13 &&  c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)) {
            System.out.println(c.getTime());
        }
        c.add(Calendar.DAY_OF_YEAR, 1);
    }

这篇关于排除打印循环的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆