这是我在java上的第一个月。我每次调用方法时都会收到一个逻辑错误,它会更新此值。如何修复此问题并使其成为静态,以便每次原始数据和时间都不会发生变化。如何在java上保存一个值
结果:
testing 7 arg constructor with initial date: [2-28-2015],[12:30:30:0]
Increasing day by 366 [2-29-2016],[12:30:30:0]
Increasing month by 12 [2-28-2017],[12:30:30:0]<---- should be 2016
Increasing year by 2 [2-28-2019],[12:30:30:0]<-------should be 2017
Initial date is [2-28-2016],[12:30:30:0]
Increasing day by 365 [2-27-2017],[12:30:30:0]
Increasing month by 11 [1-27-2018],[12:30:30:0]
Increasing year by 30 [1-27-2048],[12:30:30:0]
这里是我的代码:
public class DateTime implements DateConstants {
private Date date; // from Date Class
private Time time; // from Time class
}
public DateTime addMonths(int mo)
{
this.date.addMonths(mo);
return this;
}
public static void main(String[] myArgs) {
dateTime1 = new DateTime(2,28,2015,12,30,30,0);
System.out.println("testing 7 arg constructor with initial date: "+dateTime1);
System.out.println("Increasing day by 366 "+dateTime1.addDays(366));
System.out.println("Increasing month by 12 "+dateTime1.addMonths(12));
System.out.println("Increasing year by 2 "+dateTime1.addYears(2));
}
'静态'在这里使用完全是错误的词。它已经有了明确的含义,这不是它。 – EJP
你的意思是,public static final DateTime addMonths(int mo)? – Rishav