打印日历(java)

来源:互联网 发布:中国农村现状知乎 编辑:程序博客网 时间:2024/05/21 06:15
import java.util.Scanner;

public class DateTimeLong {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入年份:");
        int nowyear = scanner.nextInt();
        System.out.printf("请输入月份:");
        int nowmonth = scanner.nextInt();
        if(nowmonth<0||nowmonth>12){
            System.out.printf("请输入正确的月份");
        }
        else{
        
        //要求输入年份,月份;
        int leap = 0;//闰年计数变量
        int common = 0;//平年计数变量

        int timelong = 0;//从1900年1月1日至今的天数的计数变量
        int year;//年份计数变量
        for (year = 1900; year < nowyear; year++) {
            if (isLeapYear(year))
                leap++;
            else
                common++;
        }//遍历年份各天数的年份有多少天
        timelong = leap * 366 + common * 365;// 年份计算
        int a, b, c;//31天月份,30天月份。2月计数变量
        a = b = c = 0;
        int FebruarTime = 28;// 2月天数
        int month;//月份计数变量
        int monthtime = 0;// 月份天数
        for (month = 1; month <= nowmonth; month++) {
            if (month == 1 || month == 3 || month == 5 || month == 7
                    || month == 8 || month == 10 || month == 12) {
                a++;
                monthtime=31;
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                b++;
                monthtime=30;
            } else if (month == 2) {
                c++;
                monthtime=28;
                
            }

        }//判断遍历的月份中个天数的月份有多少次
        if (isLeapYear(year))
            FebruarTime = 29;// 判断是否闰年--2月29天
        timelong = timelong + 31 * a + 30 * b + FebruarTime * c + 1-monthtime;// 天数计算
        System.out.printf("天数为" + timelong + "天%n");// 輸出天數

        int week = timelong % 7;// 判断月初是星期几
        String[]arr={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        System.out.println("这个月初为"+arr[week]);//输出月初是星期几
        // System.out.println(week);
        System.out.println(nowyear+"年"+nowmonth+"月日历");
        System.out.println("Mon   Tue   Wed   Thu   Fri   Sat   Sun");// 日历头
        for (int x = 0; x < week-1; x++) { // 判断今天对应哪个日历头
            System.out.print("      "); // 此处空格数等于单个日历头的宽度
        }

//        int monthdate = monthTime(nowmonth);
        if (month == 2 && FebruarTime == 29) {
//            monthdate = 29;
            monthtime=29;
        }// 判断是否闰年2月,是的该月天数变为29天
//        for (int time = 1; time <= monthdate; time++) {
        for (int time = 1; time <= monthtime; time++) {
            int count = 0;
            System.out.printf("%2d", time);
            System.out.printf("    ");
            if ((time + week-1) % 7 == 0) {
                System.out.println("");
            }// if循环换行
        }//输出日历
        // TODO Auto-generated method stub
        }
    }
    
    static boolean isLeapYear(int year) {
        boolean result = false;
        if (year % 400 == 0)
            result = true;
        else if (year % 4 == 0 && year % 100 != 0)
            result = true;
        return result;
    }// 闰年判断

}
0 0