求解JAVA题,创建一个Date类,类中有年,月,日3个成员变量,要求实现以下功能

(1)创建构造方法初始化成员变量
(2)创建一个public void setData(int year, int month, int day)方法,更改成员变量的值,方法中的3个参数代表年,月,日。拒绝非法日期的输入。
(3)创建一个 public void addOneDay()方法实现在原有日期上加一天。
(4)创建一个public void display()方法实现用日/月/年的格式输出日期。

要求:
1.对闰年和非闰年判断
2.正确修改日期和日期加一天的情况要运行并显示
3.对年或月或日期错误的修改情况要有提示信息
4.对每月的最后一天和每年的最后一天加一天情况要运行并显 示(注意每月有30天或31天,闰年有28天或29天)

第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();
}
}本回答被提问者和网友采纳
第2个回答  2015-03-20
很简单的这个,自己先试试看!
第3个回答  2015-03-20
用日历类进行日期操作就好了,不用自己考虑闰年等
相似回答