24.蓝桥杯之输入日期

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sword_anyone/article/details/89208580

从键盘输入一个日期,格式为yyyy-M-d
要求计算该日期与1949年10月1日距离多少天
例如:
用户输入了:1949-10-2
程序输出:1
用户输入了:1949-11-1
程序输出:31


package exe21_25;

public class Exe24 {

	static int preyear = 1949;
	static int premonth = 10;
	static int preday = 1;

	public static void main(String[] args) {
		int year = 2017;
		int month = 3;
		int day = 21;

		int n = countDay(year,month,day);
		System.out.println(n);
	}

	private static int countDay(int year, int month, int day) {
		int tempYear = year;
		int countDay = 0;
		if (year>preyear) {
			year--;
			while(year>=preyear+1)
			{
				countDay += 365;
				if (year%4==0&&year%100!=0||year%400==0) {
					countDay += 1;
				}
				year--;
			}

			switch (month) {
			case 12:countDay+=30;
			case 11:countDay+=31;
			case 10:countDay+=30;
			case 9 :countDay+=31;
			case 8 :countDay+=31;
			case 7 :countDay+=30;
			case 6 :countDay+=31;
			case 5 :countDay+=30;
			case 4 :countDay+=31;
			case 3 :countDay+=28;
			if (tempYear%4==0&&tempYear%100!=0||tempYear%400==0) {
				countDay++;
			}
			case 2 :countDay+=31;
			case 1 :countDay+=day;break;
			}
			countDay+=91;
		}
		else {
			switch (month) {
			case 12:countDay+=30;
			case 11:countDay+=31;
			case 10:countDay+=day;break;
			}
		}
		return countDay-1;
	}
}

支付宝扫红包,让我能够买杯咖啡,继续创作,谢谢大家!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sword_anyone/article/details/89208580