The issue is that the int range is being exceeded. The max int range is from
-2,147,483,648 (-231) to 2,147,483,647 (231-1)
When you multiply, it is stored as a int
. But, that's not the limit. So, the solution will be to parse it to a long. You can check out my code below:
int days = 30;
long a = System.currentTimeMillis();
long b = a + ((long)days * 24 * 60 * 60 * 1000);
System.out.println("a = " + a + " b = " + b);
But if you want to optimise the code, this code will be better:
long a = System.currentTimeMillis() + ((long)30 * 24 * 60 * 60 * 1000);
System.out.println("a = " + a);
It saves your 2 lines of code
But, I'd also prefer you to use a method
long addDays(int days){
return System.currentTimeMillis() + ((long)days * 24 * 60 * 60 * 1000);
}
and then
int a = addDays(30);