求救:用java编写一个日期打印程序 实现当前日期的昨天明天的日期打印
1个回答
展开全部
public class MyDate{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public MyDate nextDay(){
int newDay = day + 1;
int newMonth = month;
int newYear = year;
if(newDay > 30){
newMonth ++;
newDay = 1;
}
if(newMonth > 12){
newYear ++;
newMonth = 1;
}
MyDate newDate =
new MyDate(newYear, newMonth, newDay);
return newDate;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
}
package ch04;
public class DateTest{
public static void main(String[] args){
MyDate oldDate = new MyDate(2005, 12, 30);
MyDate newDate = oldDate.nextDay();
System.out.println(newDate.getYear());
System.out.println(newDate.getMonth());
System.out.println(newDate.getDay());
}
}
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public MyDate nextDay(){
int newDay = day + 1;
int newMonth = month;
int newYear = year;
if(newDay > 30){
newMonth ++;
newDay = 1;
}
if(newMonth > 12){
newYear ++;
newMonth = 1;
}
MyDate newDate =
new MyDate(newYear, newMonth, newDay);
return newDate;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
}
package ch04;
public class DateTest{
public static void main(String[] args){
MyDate oldDate = new MyDate(2005, 12, 30);
MyDate newDate = oldDate.nextDay();
System.out.println(newDate.getYear());
System.out.println(newDate.getMonth());
System.out.println(newDate.getDay());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询