通过用户输入的年份与月份来算显示当月的日历

package com;
import java.util.Scanner;
public class Calendar {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int[] months = {31,28,31,30,31,30,31,31,30,31,31,30};
        int year,month;
        int dayin190011=1;
        int dayfrom190011to19991230=36525;
        int dayforyear;
        int dayformonth=0;
        int allday;
        int sum;
        int dayinthebeginofmonth;
        int sumofthismonth;
        System.out.println("请输入年份月月份");
        Scanner input=new Scanner(System.in);
        year=input.nextInt()-1900;
        month=input.nextInt();
        dayforyear=year*365+(year/4);
        if (year%4==0) {
            months[1]=29;
        }
        for (int i = 0; i < month-1; i++) {
            dayformonth+= months[i];
        }
        allday=dayformonth+dayforyear;
        sum=allday%7;
        if (sum>7) {
            sum=sum%7;
        }
        dayinthebeginofmonth=sum+1;
        if (dayinthebeginofmonth>7) {
            dayinthebeginofmonth=dayinthebeginofmonth%7;
        }
        System.out.println("本月第一天为星期"+dayinthebeginofmonth);
        sumofthismonth=months[month-1];
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        for (int i = 0; i < dayinthebeginofmonth-1; i++) {
            System.out.print(" \t");
        }
        for (int i = 1; i <=sumofthismonth; i++) {
            System.out.print(i+"\t");
            if(((dayinthebeginofmonth-1)+i)%7==0){
                System.out.println();
            }
        }
    }
}

输入的有效范围从1900年1月1日开始

猜你喜欢

转载自blog.csdn.net/u012580143/article/details/77523543