java万年历

java万年历

import java.util.Calendar;
import java.util.Scanner;

public class MyIsCalender {
    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到你输入日期的前一年的天数
        int total1 = 0;
        for (int i = 0; i < year; i++) {
            if ((year%4==0 && year%100 != 0)|| year%400 == 0) {
                total1 = total1 + 366;
            } else {
                total1 += 365;
            }

        }
        //计算输入日期的年到你输入月份的天数
        int day=0;
        int monthday = 0;//每月1月到8月天数
        for (int i = 1; i < month; i++) {
            switch (i) {
                case 2:
                    day = ((year%4==0 && year%100!=0) || year%400==0) ? 29 : 28;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = 30;
                    break;
                default:
                    day = 31;
            }
            if (i!= month)//假设输入8月i从1月循环到8月等于8月的时候则不会再去加。
            {
                monthday += day;
            }
        }
        //计算从1900到你输入日期及月份的总天数
        int totalday2 = total1 + monthday;
        //计算所输入月份的第一天是星期几
        int weekday = (totalday2 + 1) % 7;//先输出周日,则加一天
        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 < day; i++) {
            System.out.print(i + "\t");
            if ((weekday+i)%7==0)
                System.out.println();
        }
    }
}


请输入年份:
1997
请输入月份:
6
日	一	二	三	四	五	六
1	2	3	4	5	6	7	
8	9	10	11	12	13	14	
15	16	17	18	19	20	21	
22	23	24	25	26	27	28	
29	30	
进程已结束,退出代码 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值