Epoch time is the number of milliseconds that have passed since 1st January 1970, so if i want to add x
days to that time, it seems natural to add milliseconds equivalent to x
days to get the result
Date date = new Date();
System.out.println(date);
// Adding 30 days to current time
long longDate = date.getTime() + 30*24*60*60*1000;
System.out.println(new Date(longDate));
it gives the following output
Mon Dec 26 06:07:19 GMT 2016
Tue Dec 06 13:04:32 GMT 2016
I know i can use Calendar
class to solve this issue, but just wanted to understand about this behaviour
Its Because JVM is treating Value of multiplication 30*24*60*1000 as Int And multiplication result is out of range of Integer it will give result : -1702967296 intends of 2592000000 so its giving date smaller then current date
Try Below code :