-2

Why is the Result of first calculation bigger than the second calculation?

System.out.println(new Date().getTime() - (1000*60*60*24*30*3));
System.out.println(new Date().getTime() + (1000*60*60*24*30*3));

Result:

Info:   1440788283924
Info:   1439160414740 
GermanGuy
  • 15
  • 2

2 Answers2

2

The expression

1000*60*60*24*30*3

results in 32-bit integer overflow. One way to fix it is by making the first operand a long, which will force the entire expression into the long type:

1000L*60*60*24*30*3
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Integeroverflow is happening in your case and the value of 1000*60*60*24*30*3 becoming -813934592

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307