End date was being computed to be earlier than the start date
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + (24 * 3_600_000 * 42));
System.out.println(startDate);
System.out.println(endDate);
output :
Tue Sep 17 01:46:31 EEST 2013
Mon Sep 09 08:43:43 EEST 2013
why the output is not correct ?
Your integer arithmetic has overflowed. The maximum possible value of an int
is 2147483647
or Integer.MAX_VALUE
(a little over 2 billion), but multiplying your integer literals would yield 3628800000 (about 3.6 billion). The result is a negative number (-666167296
), and an earlier date.
Try casting one of your literals as a long
to force long
arithmetic (or use long
literals):
( (long) 24 * 3600000 * 42)
or
(24L * 3600000 * 42)
This operation is well within the range of long
values (max value 9223372036854775807
, over 9 quintillion).