I have a problem I can’t uderstand – if I add more than 24 days, the shifted date in the past.
long start = System.currentTimeMillis();
long shift = 3600000 * 24 * 24;
System.out.println(new Date(start));
System.out.println(new Date(start + shift));
For 24 day shift:
start - Wed Apr 18 11:37:12 CEST 2012
end - Sat May 12 11:37:12 CEST 2012
For 25 day shift:
start - Wed Apr 18 11:37:55 CEST 2012
end - Sat Mar 24 17:35:08 CET 2012
Could someone advise me why it behaves this way? Thank you.
You’re doing Integer multiplication, and then transforming the result into a long. The multiplication overflows, so the result is negative.
Use the following code to do long multiplication: