万年历

package com.test2;

import java.util.Scanner;

public class Demo2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println("请输入年份:");
		Scanner sc = new Scanner(System.in);
		int years = sc.nextInt();
		Year y = new Year(); 
		y.weekDay = y.firstWeekDay(years);
		y.year = years;
		System.out.println("\n "+years+"年      ");
		y.showMonth();
        
	}

}

class Year {
	public static int year, weekDay;

	public boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
	}

	public int firstWeekDay(int year) {

		long day = year * 365;
		for (int i = 1; i < year; i++) {
			if (isLeapYear(i)) {
				day += 1;
			}
		}
		return (int) (day % 7);
	}

	public int getMonthDay(int month) {
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		default:
			return 0;
		}
	}

	public void showMonth() {
		for (int m = 1; m <= 12; m++) {
			System.out.println(m + "月");
			System.out.println(" Sunday  Monday Tuesday Wednesday Thursday Friday Saturday");
			for (int j = 1; j <= weekDay; j++) {
				System.out.print("        ");
			}
			int monthDay = getMonthDay(m);
			for (int d = 1; d <= monthDay; d++) {
				if (d < 10) {
					System.out.print("   " + "0" + d + "   ");
				} else {
					System.out.print("   " + d + "   ");
				}
				weekDay = (weekDay + 1) % 7;
				if (weekDay == 0) {
					System.out.println();
				}
			}
			System.out.println();
		}
	
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值