public class test {
private int year ;
private int month ;
private int day ;
//用于初始化:构造函数,调用输入
public test(int year, int month, int day) {
setYear(year);
setMonth(month);
setDay(day);
}
//修改日期
public void setYear(int year) {
if(year >= 1000 && year <= 2200)
this.year = year;
else
System.out.println("error.");
}
public void setMonth(int month) {
if(month >= 1 && month <=12)
this.month = month;
else
System.out.println("error.");
}
public void setDay(int day) {
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day >= 1 && day <=31)
this.day = day;
else
System.out.println("error."); System.exit(-1);
case 4:
case 6:
case 9:
case 11:
if(day >= 1 && day <=30)
this.day = day;
else
System.out.println("error."); System.exit(-1);
case 2:
if(year%4 == 0){
if(day >= 1 && day <=29)
this.day = day;
else
System.out.println("error."); System.exit(-1);
}
else{
if(day >= 1 && day <=28)
this.day = day;
else
System.out.println("error."); System.exit(-1);
}
}
}
//返回该日期的年\月\日
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
//返回描述日期的字符串
public String showDate(){
return(getYear() + "-" + getMonth() + "-" + getDay());
}
public static void main(String[] agrs){
test d1 = new test(2009,3,16);
System.out.println(d1.showDate());
}
}