Java万年历

package com.neusoft.test;

import java.util.Scanner;

public class Test05{
	public static void main(String[] args) {
		int i;
		int j = 0;
		int year;//年
		int month;//月
		int day = 0;
		int y;//初始化年份1900
		int d = 0;//输入的天数距离1900年01月01日多少天
		int sum = 0;//输入那天是今年的多少天
		int rn;//是否闰年标记
		Scanner yinput = new Scanner(System.in);
		System.out.println("请输入查询的年份");
		year = yinput.nextInt();
		while(year < 1900||year > 9999) {
			System.out.println("输入年份有误");
			year = yinput.nextInt();
		}
		Scanner minput = new Scanner(System.in);
		System.out.println("请输入查询的月份");
		month = minput.nextInt();
		while(month < 0||month > 12) {
			System.out.println("输入月份有误");
			month = minput.nextInt();
		}
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			rn = 1;
		}else{
			rn = 0;
		}
		for(y = 1900;y<year;y++){
			if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)  {
				d = d + 366;
			}else{
				d = d + 365; 
			}
		}
		for(i = 1;i<month;i++){
			if(i == 1||i == 3||i == 5||i == 7||i == 8||i == 10||i == 12){
				j = j + 31;
				day = 31;
			}
			if (i == 4||i == 6||i == 9||i == 11) {
				j = j + 30;
				day = 30;
			}
			if (i == 2&&rn == 1) {
				j = j + 29;
				day = 29;
			}
			if (i == 2&&rn == 0){
				j = j + 28;
				day = 28;
			}
		}
		if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12){
			day = 31;
		}
		if (month == 4||month == 6||month == 9||month == 11) {
			day = 30;
		}
		if (month == 2&&rn == 1) {
			day = 29;
		}
		if (month == 2&&rn == 0){
			day = 28;
		}
		sum = j + d + 1;
		int week = sum % 7;
		String weeks[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
		if (rn == 1) {
			System.out.println("		"+year+"年"+month+"月闰年");
		}else{
			System.out.println("		"+year+"年"+month+"月平年");
		}
		System.out.println("-------------------------------------------------------");
		for(i = 0;i<7;i++){
			System.out.print(weeks[i]+"	");
		}
		System.out.println();
		for(i = 1;i<=week;i++){
			System.out.print("	");
		}
		for(j = 1;j<=day;j++){
			System.out.print(j+"	");
			if (j % 7 == 7-week) {
				System.out.println();
			}
		}
		System.out.println();
	}
}

  

原文地址:https://www.cnblogs.com/makangning/p/9381971.html