why when i add 30 days to today's day i got today's day - 30, and when i add 20 it adds??
here is a sample
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;
}
}
"this is the console"
Jul 30, 2012
Aug 19, 2012
Jul 10, 2012
解决方案
This is because 30 * 1000 * 60 * 60 * 24 overflows Integer.MAX_VALUE, while 20 * 1000 * 60 * 60 * 24 does not.