根据年份和月份打印当月日历

import java.util.Scanner;

//1900年1月1日是星期一
public class Calender {
	public static void main(String[] args) {
		System.out.println("请输入年份:");
		Scanner input=new Scanner(System.in);
		int year=input.nextInt();
		System.out.println("请输入月份");
		int month=input.nextInt();
		int total=calcYear(year);
		for(int i=0;i<month;i++){
			total=total+calcMonth(i,year);
		}
		int week=(total+1)%7;
		System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
		for(int i=0;i<week;i++){
			System.out.print("\t");
		}
		int monthDay=0;
		monthDay=calcMonth(month,year);
		for(int i=1;i<=monthDay;i++){
			System.out.print("  "+i+"\t");
			if((week+i)%7==0){
				System.out.println();
			}
		}
	}
	public static int calcYear(int year){
		int yearTotal = 0;
		for(int i = 1900;i<year;i++){
			if(runnian(i)){
				yearTotal = yearTotal + 366;
			}else{
				yearTotal = yearTotal + 365;
			}
		}
		return yearTotal;
	}
	public static boolean runnian(int year){
		boolean bool=false;
		if((year%4==0 &&year%100!=0) || (year%400==0)){
			bool=true;
		}
		return bool;
	}
	public static int calcMonth(int month,int year){
		int monthDay=0;
			switch(month){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				monthDay=31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				monthDay=30;
				break;
			case 2:
				if(runnian(year)){
					monthDay=29;
				}else{
					monthDay=28;
				}
				break;
		}
		return monthDay;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值