1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]

You are multiplying ints together, and overflow occurs because the maximum integer is 2^31 - 1. Only after the multiplications does it get converted to a long. Cast the first number as a long.

long days_30 = (long) 1000 * 60 * 60 * 24 * 30;

or use a long literal:

long days_30 = 1000L * 60 * 60 * 24 * 30;

That will force long math operations from the start.

Leave a Comment