软件构造Lab3中关于时间相关类的设计

在lab3中,不同的应用需要的时间格式不同,但IntervalSet接口统一采用了long类型作为时间点参数。为了能够使用IntervalSet接口,初步考虑使用一个时间类进行统一的转换。本来采用了Java自带的Date数据类型作为转换头,但由于Date中大部分的方法已经被舍弃了,无法作为解码器使用;除此之外,由于从给定日期到1900年1月1日的毫秒数过大,在课表APP中担心爆long,因此需要重新设计APP。最终人为定义了一个编解码器类,每个APP需要时调用作为编解码器将时间与long进行转换。排班与课表APP采用从1900年1月1日到给定时间的天数作为编码结果,保证了编码的连续性;其中课表可以将天数乘10转换为课时数,并且也保证了连续。

class DutyCodec
{
	public DutyCodec() {}
	
	/**
	 * Encode a date to a long integer
	 * @param year the year of the date
	 * @param month the month of the date
	 * @param day the day of the date
	 * @return a long integer indicates the code of the date
	 */
	public long encode(int year, int month, int day) throws DateError
	{
		long code = day - 1;
		
		if(year < 1900 || month <= 0 || day <= 0 || month > 12 || day > 31)
			throw new DateError("Invalid date");
		if(day == 31)
			if(month == 2 || month == 4 || month == 6 || month == 9 || month == 11)
				throw new DateError("Invalid date");
		if(month == 2 && day == 29)
			if(!(year % 4 == 0 && year % 100 != 0))
				throw new DateError("Invalid date");
		
		for(int i = 1900; i < year; i++)
		{
			if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
				code += 366;
			else
				code += 365;
		}
		for(int i = 1; i < month; i++)
		{
			if(!(i == 2 || i == 4 || i == 6 || i == 9 || i == 11))
				code += 31;
			else if(i != 2)
				code += 30;
			else
				code += ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) ? 29 : 28;
		}
		
		return code;
	}
	
	/**
	 * Get the year from the code
	 * @param code the code want to decode
	 * @return a integer indicates the year
	 */
	public int getYear(long code)
	{
		int year = 0;
		for(year = 1900; code >= 0; year++)
		{
			if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				code -= 366;
			else
				code -= 365;
		}
		
		return year - 1;
	}
	
	/**
	 * Get the month from the code
	 * @param code the code want to decode
	 * @return a integer indicates the month
	 */
	public int getMonth(long code)
	{
		int year = 0;
		int month = 0;
		for(year = 1900; code >= 0; year++)
		{
			if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				code -= 366;
			else
				code -= 365;
		}
		year--;
		code += ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
		
		for(month = 1; code >= 0; month++)
		{
			if(!(month == 2 || month == 4 || month == 6 || month == 9 || month == 11))
				code -= 31;
			else if(month != 2)
				code -= 30;
			else
				code -= ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 29 : 28;
		}
		
		return month - 1;
	}
	
	/**
	 * Get the day from the code
	 * @param code the code want to decode
	 * @return a integer indicates the day
	 */
	public int getDay(long code)
	{
		int year = 0;
		int month = 0;
		for(year = 1900; code >= 0; year++)
		{
			if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				code -= 366;
			else
				code -= 365;
		}
		year--;
		code += ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
		
		for(month = 1; code >= 0; month++)
		{
			if(!(month == 2 || month == 4 || month == 6 || month == 9 || month == 11))
				code -= 31;
			else if(month != 2)
				code -= 30;
			else
				code -= ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 29 : 28;
		}
		month--;
		if(!(month == 2 || month == 4 || month == 6 || month == 9 || month == 11))
			code += 31;
		else if(month != 2)
			code += 30;
		else
			code += ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 29 : 28;
		
		return (int) code + 1;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值