控制台输入4位年份,打印出整年的日历

 Scanner input = new Scanner(System.in);
        int year,month,day= 0;
        long total = 0;
        System.out.println("请输入4位年");
        year = input.nextInt();
        for (int i = 1900; i <year ; i++) {
            total += (i%4==0&&i%100!=0)||i%400==0?366:365;
        }
        total %= 7; //计算上一年的最后一天为星期几
        final  String WEEK_HEAD = "日	一	二	三	四	五	六";
        //验证输出
        for (int i = 1; i <=12; i++) {
            System.out.println("
["+i+"月]");
            int days = 31;
            switch(i){
                case 4: case 6: case 9: case 11:
                days = 30;
                break;
                case 2:
                    days =(year%4==0&&year%100!=0)||year%400==0?29:28;
                    break;
            }
            //扫描所有的天数,验证是周几,是否是新的一周
            System.out.println(WEEK_HEAD);
            //输出的是当月第一天之前	
            total = total== 6?-1:total;
            for (int j = 0; j <=total ; j++) {
                System.out.print("	");
            }
            //当月日期的输出
            for (int j = 1; j <=days ; j++) {
                if(++total%7==0&&j!=1){
                    System.out.println();
                }
                System.out.print(j+"	");
            }
            System.out.println();
            total %= 7;
        }
原文地址:https://www.cnblogs.com/chenyyStudy/p/12943113.html