Java、MyDate类

在这里插入图片描述

 


UML类图:

————————————————————————————————————————
                              MyDate
————————————————————————————————————————
-year: int
-month: int
-day: int
————————————————————————————————————————
+MyDate()
+MyDate(elapsedTime: long)
+MyDate(year: int, month: int, day: int)
+setDate(elapsedTime: long): void
+getYear(): int
+getMonth(): int
+getDay(): int
————————————————————————————————————————


package pack2;

import java.util.GregorianCalendar;

public class MyDate {
	private int year, month, day;	//年、月、日

	/**当前日期的无参构造方法*/
	public MyDate() {
		setDate(System.currentTimeMillis());
	}

	/**以流逝的毫秒数为时间的构造方法*/
	public MyDate(long elapsedTime) {
		setDate(elapsedTime);
	}

	/**带指定年、月、日的构造方法*/
	public MyDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	/**使用流逝的时间设置新日期*/
	public void setDate(long elapsedTime) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(elapsedTime);
		
		year = calendar.get(GregorianCalendar.YEAR);
		month = calendar.get(GregorianCalendar.MONTH);
		day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
	}

	@Override	/**返回年、月、日的字符串*/
	public String toString() {
		return "Year: " + year + "\nMonth: " + month + "\nDay: " + day;
	}
	
	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}
	
//————————————————————————————————————————————————————
	public static void main(String[] args) {
		MyDate date1 = new MyDate();
		MyDate date2 = new MyDate(34355555133101L);
		
		System.out.println("date1: \n" + date1);
		System.out.println("\ndate2: \n" + date2);
		date2.setDate(561555550000L);
		System.out.println("\ndate2: \n" + date2);
	}
}

 


 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值