关于java 日期和时间的迁移应用

本文探讨了如何在Java中处理日期和时间,重点在于接收用户输入的天数并进行相应计算。文章指出需考虑年份、月份和天数的限制,如年份区间、月份范围及闰年对天数的影响。提供了相关的源代码实现。

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

 要求:存取年月日的信息     

从用户角度出发 存入数据后 由用户规定传入天数,计算机做出相应的计算

这个问题我们要考虑年月日的限制条件以及递推关系

1.年份需要写前给出区间,在进行判断。

2.月份在一到十二月之间。

3.天数的限定在每月的天数可能不同,需要进行设计。(考虑闰年的问题)

下面是源代码:

/*
 * Date 	存储 年-月-日 信息
 * 原则: 一切从用户角度出发
 * 功能:
 *		1) 初始化
 *			i.	传入年/月/日
 *			2.  不传,今天		回头
 *		2) 多少天之后的年/月/日
 *		3) 多少天之前的年/月/日
 */
public class Date {
	public int year;
	public int month;
	public int day;
	
	public int[] day_of_month = {
		31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	};
	
	// 构造方法
	// 年支持的范围 [1840, 2020]
	// 月支持的范围 [1, 12]
	// 日支持的范围
	public Date(int year, int month, int day) {
		// 用户传入参数的合法性校验
		if (year < 1840 || year > 2020) {
			System.err.println("年的支持范围是 [1840, 2020]");
			return;
		}
		
		if (month < 1 || month > 12) {
			System.err.println("不是地球人的月份");
			return;
		}
		
		if (day < 1 || day > calcDaysOfMonth(year, month)) {
			System.err.println("天数不对");
			return;
		}
		
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	public int calcDaysOfMonth(int year, int month) {
		if (month != 2) {
			return day_of_month[month - 1];
		}
		
		if (isLeapYear(year)) {
			return 29;
		} else {
			return 28;
		}
	}
	
	public boolean isLeapYear(int year) {
		if (year % 4 == 0 && year % 100 != 0) {
			return true;
		}
		
		if (year % 400 == 0) {
			return true;
		}
		
		return false;
	}
	
	public Date after(int days) {
		day += days;
		
		while (day > calcDaysOfMonth(year, month)) {
			day -= calcDaysOfMonth(year, month);
			month += 1;
			
			if (month > 12) {
				month = 1;
				year += 1;
			}
		}
		
		return this;
	}
	
	public Date before(int days) {
		day-=days;
		while(day>calcDaysOfMonth(year,month))
		{
			days-=calcDaysOfMonth(year,month);
			month-=1;
			if(month<1){
				month=12;
				year-=1;
			}
		}
		return this;
	}
	
	public String toString() {
		return String.format("%04d-%02d-%02d", year, month, day);
	}
	
	public static void main(String[] args) {
		Date d = new Date(2019, 7, 20);
		Date r = d.after(80);
		System.out.println(r.toString());
	}
}




















根据上述代码,总结后,我们可以得到时分秒的转换:

代码如下:

package wxl;
public class Time {
	public int hour;
	public int minute;
	public int second;	
public Time(int hour,int minute,int second)
{
	if(hour<0||hour>23)
	{
		System.err.println("小时的支持范围是 [0, 23]");
		return;
	}
	if (minute < 0 || minute > 59) {
		System.err.println("不是地球人的分");
		return;
	}
	if(second<0||second>59)
	{
		System.err.println("不是地球人的秒");
		return;
	}
	this.hour=hour;
	this.minute=minute;
	this.second=second;
}
public Time after(int seconds) {
	second+=seconds;
	while(second>=60)
	{
		second-=60;
		minute++;
		if(minute>=60)
		{
			minute-=60;
			hour++;
		}
		if(hour>=24)
		{
			hour=hour%24;
		}
	}	
	return this;
}
public String toString() {
	return String.format("%4d:%02d:%02d", hour, minute, second);
}
public static void main(String[] args) {
	Time d = new Time(23,58,58);
	Time r = d.after(7200);
	System.out.println(r.toString());
}
}

	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值