第1个回答 推荐于2017-09-17
package arraylist;
public class Date
{
private int year;
private int month;
private int day;
public Date(int year, int month, int day)
{
this.setYear(year);
this.setMonth(month);
this.setDay(day);
}
public void setDate(int year, int month, int day)
{
this.setYear(year);
this.setMonth(month);
this.setDay(day);
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
if (year > 0 && year < 2015)
{
this.year = year;
}
else
{
year = -1;
}
}
public int getMonth()
{
return month;
}
public void setMonth(int month)
{
if (month > 0 && month <= 12)
{
this.month = month;
}
else
{
month = -1;
}
}
public int getDay()
{
return day;
}
public void setDay(int day)
{
if (day >= 1 && day <= 31)
{
this.day = day;
}
else
{
day = -1;
}
}
public void addOneDay()
{
switch (this.getMonth())
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (this.getDay() != 31)
{
this.setDay(this.getDay() + 1);
}
else
{
if (this.getMonth() == 12)
{
this.setYear(this.getYear() + 1);
this.setMonth(1);
this.setDay(1);
}else{
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (this.getDay() != 30)
{
this.setDay(this.getDay() + 1);
}
else
{
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
break;
case 2:
if (this.getYear() % 4 == 0 && this.getYear() % 100 != 0 || this.getYear() % 400 == 0)
{
if (this.getDay() != 29)
{
this.setDay(this.getDay() + 1);
}
else
{
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
}else{
if (this.getDay() != 28)
{
this.setDay(this.getDay() + 1);
}
else
{
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
}
}
}
public void display(){
System.out.println("你需要的日期是" + this.getYear() + "年" + this.getMonth() + "月" + this.getDay() + "日");
}
public static void main(String [] args){
Date d = new Date(1999, 11, 30);
d.display();
d.addOneDay();
d.display();
}
}本回答被提问者和网友采纳