永发信息网

求救:用java编写一个日期打印程序 实现当前日期的昨天明天的日期打印

答案:2  悬赏:70  手机版
解决时间 2021-02-24 20:38
  • 提问者网友:愿为果
  • 2021-02-24 14:30
求救:用java编写一个日期打印程序 实现当前日期的昨天明天的日期打印
最佳答案
  • 五星知识达人网友:旧脸谱
  • 2021-02-24 15:59
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());
}
}
全部回答
  • 1楼网友:毛毛
  • 2021-02-24 16:55
当然会报错 nuberformatexception 你都定义了接收的数据类型为int, 却输入string 类型值赋给int,,,, 肯定要报错的..
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯