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);
}
}