为什么当我在今天加上 30 天时,我得到了今天 - 30,当我加上 20 时,它增加了?
这是一个样本
import java.text.DateFormat;
import java.util.Date;
public class DatePlus
{
public static void main(String[] args)
{
Date now = new Date();
Date now1 = new Date();
Date now2 = new Date();
DateFormat currentDate = DateFormat.getDateInstance();
Date addedDate1 = addDays(now2, 20);
Date addedDate2 = addDays(now1, 30);
System.out.println(currentDate.format(now));
System.out.println(currentDate.format(addedDate1));
System.out.println(currentDate.format(addedDate2));
}
public static Date addDays(Date d, int days)
{
d.setTime(d.getTime() + days * 1000 * 60 * 60 * 24);
return d;
}
}
“这是控制台”
Jul 30, 2012
Aug 19, 2012
Jul 10, 2012