JAVA 万年历from1900.1.1

1.目的

用户输入年份+月份 控制台输出那一年的日历格式。
控制台输出内容显示

2.具体实现

一:获取用户输入

		Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = sc.nextInt();
        System.out.println("请输入月份");
        int month = sc.nextInt();

二:根据用户的输入,计算1900.1.1到输入月份1号的天数(这里不包括1号的天数)

		//获得从1900年1月1日 到输入月份1日的总天数(不包括1号)
        int totalDays = getTotalDaysFrom1990(year, month);
        //计算1号是星期几
        int week = (totalDays+1) % 7;//星期日是,week = 0

具体代码实现

public static int getTotalDaysFrom1990(int year,int month){
        if(year<1900){
            System.out.println("Sorry 只能查看1900之后的Calendar");
            System.exit(0);
        }
        int totalDays = 0;
        //先计算到输入年份1月1日之前的总天数 然后再加上输入年份的天数
        for (int i = 1900; i < year ; i++) {
       		 //闰年是366天 这里要判断是否为闰年
            totalDays += isRunYear(i) ? 366 :365;
        }
        //这里计算的是输入年份那几个月的总天数 在加上之前的就是一共的天数
        //如2019年11月 上面把2019年之前的天数算出来了 这里再加上2019年11月之前的天数
        for (int i = 0; i < month-1; i++) {
            totalDays +=getMonthDay(year, i);
        }
        return totalDays;
    }

判断是否为闰年

/**
     * 闰年366 平年365 公历闰年判定遵循的规律为: 
     * 四年一闰,百年不闰,四百年再闰。
     * 公历闰年的简单计算方法(符合以下条件之一的年份即为闰年)
     * 1.能被4整除而不能被100整除。
     * 2.能被100整除也能被400整除。* */
    public static boolean isRunYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
            return true;//是闰年
        }
        return false;// 是平年
    }

获取每个月份的天数 (区分大小月and闰年与平年的2月的天数是不同的)

//获得每个月份的天数
    private static int getMonthDay(int year, int month) {
        switch (month) {
            default:
                return 31;
            case 4: case 6: case 9: case 11:
                return 30;
            case 2:
                return isRunYear(year) ? 29 : 28;
        }
    }

打印输出

int count = 0;
        System.out.println("星期日\t\t星期一\t\t星期二\t\t星期三\t\t星期四\t\t星期五\t\t星期六");
        //2)打印空白
        for(int i=1;i<= week;i++){
            System.out.print("\t\t\t");
            count++;
        }
        //3) 打印日历
        for(int i=1;i<=getMonthDay(year,month);i++){
            System.out.print(i+"\t\t\t");
            count++;
            //若记录空白数和日期数的和是七的倍数,应该换行输出
            if(count % 7 == 0){
                System.out.println();
            }
        }

完整代码

public class Calendar {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = sc.nextInt();
        System.out.println("请输入月份");
        int month = sc.nextInt();

        //获得从1900年1月1日 到输入月份1.1日的总天数
        int totalDays = getTotalDaysFrom1990(year, month);
        int week = (totalDays+1) % 7;//星期日是,week = 0



        int count = 0;
        System.out.println("星期日\t\t星期一\t\t星期二\t\t星期三\t\t星期四\t\t星期五\t\t星期六");
        //2)打印空白
        for(int i=1;i<= week;i++){
            System.out.print("\t\t\t");
            count++;
        }
        //3) 打印日历
        for(int i=1;i<=getMonthDay(year,month);i++){
            System.out.print(i+"\t\t\t");
            count++;
            //若记录空白数和日期数的和是七的倍数,应该换行输出
            if(count % 7 == 0){
                System.out.println();
            }
        }

    }
    //获得每个月份的天数
    private static int getMonthDay(int year, int month) {
        switch (month) {
            default:
                return 31;
            case 4: case 6: case 9: case 11:
                return 30;
            case 2:
                return isRunYear(year) ? 29 : 28;
        }
    }
    /**
     * 先判断是否为闰年366 平年365 公历闰年判定遵循的规律为: 四年一闰,百年不闰,四百年再闰。
     * 公历闰年的简单计算方法(符合以下条件之一的年份即为闰年)
     * 1.能被4整除而不能被100整除。
     * 2.能被100整除也能被400整除。* */
    public static boolean isRunYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
            return true;//是闰年
        }
        return false;// 是平年
    }
    public static int getTotalDaysFrom1990(int year,int month){
        if(year<1900){
            System.out.println("Sorry 只能查看1900之后的Calendar");
            System.exit(0);
        }
        int totalDays = 0;
        for (int i = 1900; i < year ; i++) {
            totalDays += isRunYear(i) ? 366 :365;
        }
        for (int i = 0; i < month-1; i++) {
            totalDays +=getMonthDay(year, i);
        }
        return totalDays;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值