java50题----14第几天

本文介绍了一个Java程序,用于计算输入的日期(年月日)是一年中的第几天,考虑了平年和闰年的情况,并验证了输入的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
输入某年某月某日,判断这一天是这一年的第几天。

*/


import java.io.*;

class Demo
{
	private Demo(){}

	private static Demo instance = new Demo();

	private boolean yearflag = false;

	private	int[] monthdays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};	
	
	
	public static Demo getInstance()
	{
		return instance;
	}
 

	public int getDay(int year, int month, int day)
	{

		if(isValidDate(year, month, day) != true)
			return -1;

		setYearFlag(year);

		if(this.yearflag == true)
			this.monthdays[1] = 29;
		else
			this.monthdays[1] = 28;

		int sum = 0;
		for(int i = 1; i<month; i++)
		{
			sum = sum + monthdays[i-1];
		}

		return sum + day;

	}


	private void setYearFlag(int year)
	{
		this.yearflag = isRunNian(year);
	}

	public boolean isRunNian(int year)
	{
		if( (year%400 == 0) || ( (year%4 == 0) && (year%100 != 0)) )
		{
			return true;
		}
		else
			return false;
	}

	private boolean isValidDate(int year, int month, int day)
	{
		if(isValidYear(year) && isValidMonth(month) && isValidDay(year, month, day))
			return true;
		else
			return false;
	}


	private boolean isValidYear(int year)
	{
		if(year >= 1 && year <= 3200)
			return true;
		else
			return false;
	}

	private boolean isValidMonth(int month)
	{
		if(month >= 1 && month <= 12)
			return true;
		else
			return false;
	}

	private boolean isValidDay(int year, int month, int day)
	{
		if(day <= 0 || day >= 32)
			return false;

		switch(day)
		{
			case(29):
				if( (month == 2) && (isRunNian(year) ) )
					return true;
				break;
			case(30):
				if(month != 2)
					return true;
				break;
			case(31):
				if(month != 2 && month != 4 && month != 6 && month != 9 && month != 11)
					return true;
				break;
			default:
				return true;
		}


		return false;
	}




}

class MainClass 
{
	public static void main(String[] args) throws Exception
	{
		Demo d = Demo.getInstance();
		
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));

		String reg = "\\d+-\\d+-\\d+";
		String regsplit = "-";
		String[] arr = new String[3];

		int year = 0;
		int month = 0;
		int day = 0;

		System.out.println("输入一个日期,例如:1970-1-1:");


		for(String str = buf.readLine().trim();true; str = buf.readLine().trim())
		{
			if(str.isEmpty() == true)
				continue;

			if(str.equals("quit") == true)
				System.exit(0);

			if(str.matches(reg) == true)
			{
				arr = str.split(regsplit);

				year = Integer.parseInt(arr[0]);
				month = Integer.parseInt(arr[1]);
				day = Integer.parseInt(arr[2]);

				int days = d.getDay(year, month, day);
			
				if(days != -1)
				{
					System.out.println(str+"\t是"+year+"年的"+"第"+days+"天");	
				}
				else
					System.out.println("输入了非法的日期!");

			}
			else
			{
				System.out.println("输入的日期格式不对!例如:1970-1-1");
			}


		}
	}
}


/*
闰年的计算,归结起来就是通常说的:四年一闰;百年不闰,四百年再闰。



*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值