万年历:输入year年,month月,显示其日历

//万年历功能类
public class MyCalender {
    //判断润平年功能
    public boolean isRun(int year){
        //普通年,被4整除,且%100!=0,是闰年
        if (((year%4)==0) && ((year%100)!=0)){
            //System.out.println(year+"是闰年");
            return true;
        }
        if (year%400==0){
            //System.out.println(year+"是闰年");
            return true;
        }
        //System.out.println(year+"是平年");
        return false;

    }
    //输入一个年,月,返回这个月有多少天
    public int getDays(int year, int month){
        int days=30;
        if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            days=31;
        }else if (month==4||month==6||month==9||month==11){
            days=30;
        }else {
            boolean r=isRun(year);
            if (r){
                days=29;
            }else {
                days=28;
            }
        }
        return days;
    }
    //1900,1,1日是周1,往后算,所要计算的年月日到1900,1,1日多少天,与%7,即可的到
    //输入年月,计算这个year,month,1号到1900,1,1总共有多少天
    private int getTotal(int year,int month){
        int total=0;
        //循环从1900到year-1年,的12月31日,在这个循环中判断每次的值是闰年还是平年,闰年则+366,否则365
        //计算从year 年到1900年中有多少天
        for (int i=1900;i<year;i++){
            if (isRun(i)){
                total+=366;
            }else {
                total+=365;
            }
        }
        //计算yearn年moth月,到year年1月有多少天
        //从1到12循环,累加getDays(年,月)
        for (int j=1;j<month;j++){
            total+=getDays(year, j);
        }
        //在加1
        return total+1;
    }
    //求某个总天数对应是周几
    private int getWeekday(int total){
        return total%7;
    }
    public void showCalender(int year,int month){
        int days=getDays(year,month);//year年month月有多少天
        System.out.println(year+"年"+month+"月有:"+days+"天");
        int total=getTotal(year,month);
        System.out.println("到1900年1月1日总共有:"+total+"天");
        int weekday=getWeekday(total);
        System.out.println("周"+weekday);
        //显示日历
        System.out.println(year+"年"+month+"月");
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        for (int i=0;i<weekday;i++){
            System.out.print("\t");
        }
        for (int i=1;i<=days;i++){
            System.out.print(i+"\t");
            if ((i+weekday)%7==0){//(月份号数+周几数)%7==0,换行
                System.out.println();
            }
        }
    }

}
//main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值