0
基本上,我有一个使用日期的小程序。更改日期
Date current = new Date();
current.setDate(current.getDay() + time1);
当我做到这一点增加了一天,但说时间1 = 30然后当我打印日期从本月不会改变。我希望这是有道理的,我有点新鲜。
基本上,我有一个使用日期的小程序。更改日期
Date current = new Date();
current.setDate(current.getDay() + time1);
当我做到这一点增加了一天,但说时间1 = 30然后当我打印日期从本月不会改变。我希望这是有道理的,我有点新鲜。
使用Calendar
执行日期算术,并使用DateFormat
来显示结果。喜欢的东西,
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 30);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(cal.getTime()));
使用此方法
public static Date addDaystoGivenDate(Integer days, Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
使用.getDate代替.getDay。 getDay返回的值是一个与星期几对应的整数:0表示星期日,1表示星期一,2表示星期二,等等。 – WannaBeGeek
许多Date方法已被弃用。日历有一个roll()函数,可以完成你要找的内容,但我强烈建议寻找Joda Time。 (http://www.joda.org/joda-time/) – haley
您正在使用不推荐的'Date'方法。请参阅http://stackoverflow.com/questions/2901262/why-were-most-java-util-date-methods-deprecated – Raedwald