Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtraction of System.currentTimeMillis()

Tags:

java

    long rt = System.currentTimeMillis()-(1000*60*60*24*30);

When i convert the above variable rt to date i am not able to get expected date(i.e)30 days before. why?

like image 851
Exodus Avatar asked Oct 20 '25 04:10

Exodus


1 Answers

1000*60*60*24*30 results in int overflow, since it's larger than Integer.MAX_VALUE. Change it to 1000L*60*60*24*30 to use long instead.

For example:

long rt = System.currentTimeMillis()-(1000L*60*60*24*30);
System.out.println (new Date(rt));

prints for me:

Sun Feb 25 09:18:58 IST 2018
like image 124
Eran Avatar answered Oct 21 '25 17:10

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!