写一个类 Date,要求存储基本的年月日信息,
原则: 一切从用户角度出发
功能:
1) 初始化-----传入年/月/日
2) 多少天之后的年/月/日
代码如下:
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
};
// 构造方法
// 年支持的范围 [1800, 2050]
// 月支持的范围 [1, 12]
// 日支持的范围
public Date(int year, int month, int day) {
// 用户传入参数的合法性校验
if (year < 1800 || year > 2050) {
System.err.println("年的支持范围是 [1800, 2050]");
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 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());
}
}