0

The value of Long.MAX_VALUE is 9223372036854775807 However, when I print the following:

System.out.println(1000 * 60 * 60 * 24 * 30);

I get -1702967296

Although the value should be 2592000000 which is smaller than Long.MAX_VALUE

Morad
  • 2,761
  • 21
  • 29
  • 5
    use `1000L * 60 * 60 * 24 * 30` – Eran Jun 19 '19 at 08:33
  • 2
    Currently you have an integer operation which overflows because result > `Integer.MAX_VALUE` this Erans solution is to be used – XtremeBaumer Jun 19 '19 at 08:34
  • Thanks... I missed that :) – Morad Jun 19 '19 at 08:36
  • 1
    Or, better in terms of expressing the intent, use `TimeUnit.DAYS.toMillis(30)`. (Worse in terms of that not being a constant expression). – Andy Turner Jun 19 '19 at 08:37
  • try use System.out.println(1000L * 60L * 60L * 24L * 30L); or System.out.println(1000L * 60 * 60 * 24 * 30); java will convert to long value from smaller to bigger in second example – yali Jun 19 '19 at 09:04

1 Answers1

3

The values you are using are int not long. Use long value otherwise the result wraps round and becomes negative.

mwarren
  • 759
  • 3
  • 6