万年日历思路加完整代码

1、如何判断闰年 解决哪些年是365天,哪些年是366天
能被4整除且不能被100整除 或者 可以被400整除
编写方法给定年份,返回该年共有多少天

    /**
     * 
     * @param year 传入的年份
     * @return 返回的是传入年份有多少天
     */

public static int YearDays(int year)

2、判定2月是28天还是29天,哪些月是30天,那些月是31天
由于28天和29天情况 ,一定得知道要判定的是哪一年
30天 4 6 9 11
31天 1 3 5 7 8 10 12
28 2 月平年
29 2月闰年

	/**
	 * 
	 * @param year 传入的年份
	 * @return 判断传入的年份是否是闰年,true表示是闰年,false表示为平年
	 */
public static boolean isLeapYear(int year)

    /**
 * 
 * @param month 月份
 * @param year 年份
 * @return 当年的月份有多少天
 */




public static int MonthDays(int month,int year)

**3、判定每个1号是周几
1900开始计时
1900年1月1号就是星期一
1月是31天 31 % 7 = 3 余数 + 1 就是2月1号周几 2月1号周4
假如要计算3月1号
1月31天 2月28天 (31+28) % 7 = 3 周四
加入要计算4月1号
(31+28+31) % 7 = 6 余数 + 1 = 7

加入要计算1903年 3月1号是周几 
(1900年天数 + 1901年的天数 + 1902年的天数 + 1903年中 1月,2月的天数) % 7
(365+365+365+31+28) % 7 = 1154 % 7 = 6  周日**

public static int  WeekDays(int month,int year)

4、打印日历
输入一个年份 ,打印全年的日历

	/**
 * 
 * @param year 输入的年份
 */
public static void printcalender(int year)

JAVA完整代码展示

package calendar1;

import java.util.Scanner;

public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner read=new Scanner(System.in);
		System.out.println("请输入年份:");
		int year=read.nextInt();
		int days=YearDays(year);
		System.out.println(year+"年有"+days+"天!");
		System.out.println("请输入月份:");
		int month=read.nextInt();
		int days2=MonthDays(month, year);
		System.out.println(year+"年"+month+"月有"+days2+"天!");
		System.out.println(year+"年"+month+"月1日是周"+(WeekDays(month, year)+1));
		printcalender(year);
	}
	
	/**
	 * 
	 * @param year 传入的年份
	 * @return 返回的是传入年份有多少天
	 */
	public static int YearDays(int year)
	{
		int days=0;
		return days=isLeapYear(year)?366:365;
	}
	
	/**
	 * 
	 * @param year 传入的年份
	 * @return 判断传入的年份是否是闰年,true表示是闰年,false表示为平年
	 */
	public static boolean isLeapYear(int year)
	{
		boolean  flag=false;
		if(((year%100==0)&&(year%400==0))||((year%100!=0)&&(year%4==0)))
		{
			flag=true;
		}
		else
		{
			return flag=false;
		}
		
		return flag;
	}
	
	
	/**
	 * 
	 * @param month 月份
	 * @param year 年份
	 * @return 当年的月份有多少天
	 */
	public static int MonthDays(int month,int year)
	{
		int days=0;
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
		{
			days=31;
		}
		else if(month==4||month==6||month==9||month==11)
		{
			days=30;
		}
		else if(month==2&&isLeapYear(year))
		{
			days=29;
		}
		else if(month==2&&!isLeapYear(year))
		{
			days=28;
		}
		return days;
	}
	
	public static int  WeekDays(int month,int year)
	{
		int days=0;
		/**
		 * 先计算年份的天数
		 */
		for (int i = 1990; i <year; i++) {
			days+=YearDays(year);
		}
		/**
		 * 然后计算月份的天数
		 */
		for (int i = 1; i < month; i++) {
			days+=MonthDays(i, year);
		}
		return days%7;
	}
	
	/**
	 * 
	 * @param year 输入的年份
	 */
	public static void printcalender(int year)
	{
		for (int i = 1; i <= 12; i++) {
			System.out.println("\t\t\t"+i+"月");
			System.out.println();
			System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t");
			for (int j = 0; j <=WeekDays(i, year); j++) {
				System.out.print("\t");
			}
			for (int t =1; t <= MonthDays(i, year); t++) {
				if ((t+WeekDays(i, year))%7==0) {
					System.out.println();
				}
				System.out.print(t+"\t");
			}
			System.out.println();
			System.out.println();
		}
	}
}

运行的效果:
*请输入年份:
2019
2019年有365天!
请输入月份:
2
2019年2月有28天!
2019年2月1日是周5
1月
日 一 二 三 四 五 六
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 31
2月
日 一 二 三 四 五 六
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
3月
日 一 二 三 四 五 六
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
31
4月
日 一 二 三 四 五 六
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
5月
日 一 二 三 四 五 六
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 31
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
7月
日 一 二 三 四 五 六
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 31
8月
日 一 二 三 四 五 六
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 31
9月
日 一 二 三 四 五 六

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
10月
日 一 二 三 四 五 六
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 31
11月
日 一 二 三 四 五 六
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
12月
日 一 二 三 四 五 六

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 31*

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值