【项目三】时间信息的类MyDateTime

题目描述

  • 定义一个描述时间信息的类MyDateTime:
    属性:year,month,day,hour,minute,second
    方法:
    • 构造方法:3个
    1. 分别将6个属性设置为0;
    2. 根据6个整型形参设置属性;
    3. 根据另一个MyDateTime对象的属性进行设置;
    • 其他方法:
    1. MyDateTime passTime(int length,int type)(在当前时间的基础上加上length时间段,时间段单位由type决定:1-6分别代表年/月/日/时/分/秒)
    2. int diffDateTime(MyDateTime dayx ,int measure )(计算当前时间与参数对象dayx之间相差的时间段,时间段单位由measure决定:1-3分别代表天/时/秒)
    3. int dayInYear()(计算当前时间是一年中的第几天)
  • 在另一个类中使用此类的对象,验证其正确性。

代码实现
  1. MyDateTime.java
public class MyDateTime {

	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private int second;

	/**
	 * 构造方法:分别将6个属性设置为0
	 */
	public MyDateTime() {
		super();
		this.year = 0;
		this.month = 0;
		this.day = 0;
		this.hour = 0;
		this.minute = 0;
		this.second = 0;
	}

	/**
	 * 构造方法:根据6个整型形参设置属性
	 * 
	 * @param year   年
	 * @param month  月
	 * @param day    日
	 * @param hour   时
	 * @param minute 分
	 * @param second 秒
	 */
	public MyDateTime(int year, int month, int day, int hour, int minute, int second) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	/**
	 * 构造方法:根据另一个MyDateTime对象的属性进行设置
	 * 
	 * @param myDateTime MyDateTime对象
	 */
	public MyDateTime(MyDateTime myDateTime) {
		this.year = myDateTime.year;
		this.month = myDateTime.month;
		this.day = myDateTime.day;
		this.hour = myDateTime.hour;
		this.minute = myDateTime.minute;
		this.second = myDateTime.second;
	}

	/**
	 * 在当前时间的基础上加上length时间段,时间段单位由type决定:1-6分别代表年/月/日/时/分/秒
	 * 
	 * @param length 时间段
	 * @param type   类型
	 * @return
	 */
	public MyDateTime passTime(int length, int type) {
		if (type == 1) {
			this.year += length;
		} else if (type == 2) {
			this.month += length;
		} else if (type == 3) {
			this.day += length;
		} else if (type == 4) {
			this.hour += length;
		} else if (type == 5) {
			this.minute += length;
		} else if (type == 6) {
			this.second += length;
		}
		return this;
	}

	/**
	 * 计算当前时间与参数对象dayx之间相差的时间段,时间段单位由measure决定:1-3分别代表天/时/秒
	 * 
	 * @param dayx
	 * @param measure
	 * @return
	 */
	public int diffDateTime(MyDateTime dayx, int measure) {
		if (measure == 1) {
			return dayx.day - this.day;
		} else if (measure == 2) {
			return dayx.hour - this.hour;
		} else if (measure == 3) {
			return dayx.second - this.second;
		} else {
			return 0;
		}
	}

	/**
	 * 计算当前时间是一年中的第几天
	 * 
	 * @return
	 */
	public int dayInYear() {
		int dayOfYear = this.day;
		for (int i = 0; i < this.month; i++) {
			switch (this.month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
				dayOfYear += 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				dayOfYear += 30;
			case 2:
				if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) {
					dayOfYear += 29;
				} else {
					dayOfYear += 28;
				}
			default:
				break;
			}
		}
		return dayOfYear;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}

	@Override
	public String toString() {
		return " [year=" + year + ", month=" + month + ", day=" + day + ", hour=" + hour + ", minute="
				+ minute + ", second=" + second + "]";
	}

}

  1. MyDateTimeTest.java
public class MyDateTimeTest {
	public static void main(String[] args) {
		// 构造方法测试
		// 分别将6个属性设置为0
		MyDateTime myDateTime1 = new MyDateTime();
		System.out.println(myDateTime1.toString());
		// 根据6个整型形参设置属性
		MyDateTime myDateTime2 = new MyDateTime(2020, 3, 28, 15, 23, 25);
		System.out.println(myDateTime2.toString());
		// 根据另一个MyDateTime对象的属性进行设置;
		MyDateTime myDateTime3 = new MyDateTime(myDateTime2);
		System.out.println(myDateTime3.toString());
		System.out.println();
		// passTime测试
		myDateTime1.passTime(10, 1);
		System.out.println("myDateTime1.passTime(10, 1);" + myDateTime1.toString());
		myDateTime1.passTime(10, 2);
		System.out.println("myDateTime1.passTime(10, 2);" + myDateTime1.toString());
		myDateTime1.passTime(10, 3);
		System.out.println("myDateTime1.passTime(10, 3);" + myDateTime1.toString());
		myDateTime1.passTime(10, 4);
		System.out.println("myDateTime1.passTime(10, 4);" + myDateTime1.toString());
		myDateTime1.passTime(10, 5);
		System.out.println("myDateTime1.passTime(10, 5);" + myDateTime1.toString());
		myDateTime1.passTime(10, 6);
		System.out.println("myDateTime1.passTime(10, 6);" + myDateTime1.toString());
		System.out.println();
		// diffDateTime测试
		MyDateTime dateTime = new MyDateTime(10, 11, 12, 13, 14, 15);
		System.out.println("myDateTime1" + myDateTime1);
		System.out.println("dateTime" + dateTime);
		int dayDiff = myDateTime1.diffDateTime(dateTime, 1);
		System.out.println("相差天数:" + dayDiff);
		int hourDiff = myDateTime1.diffDateTime(dateTime, 2);
		System.out.println("相差小时数:" + hourDiff);
		int secondDiff = myDateTime1.diffDateTime(dateTime, 3);
		System.out.println("相差秒数:" + secondDiff);
		System.out.println();
		// dayInYear测试
		int dayInYear = myDateTime1.dayInYear();
		System.out.println("myDateTime1一年中的第几天:" + dayInYear);
	}
}

运行结果展示

在这里插入图片描述


本节完!

更多信息交流请加QQ:1406073270
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值