Date类:
//import SavingsAccount.Date;
public class Date{//日期类
private int year;//年
private int month=1;//月
private int day;//日
private int totalDays;//该日期是从公元元年1月1日开始的第几天
final int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
public Date(int year,int month,int day) {
if (day <= 0 || day > getMaxDay()) {
System.out.print("Invalid date: ");
show();
System.out.println();
System.exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
};
public final int getYear(){
return year;
}
public final int getMonth(){
return month;
}
public final int getDay(){
return day;
}
public final void show() {
System.out.print(getYear()+"-"+getMonth()+"-"+getDay());
}
public final boolean isLeapYear(){//判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public final int distance(final Date date){
return totalDays - date.totalDays;
}
public int getMaxDay() {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}
}
//import SavingsAccount.Date;