hellzone hellzone - 29 days ago 7
Java Question

How to calculate 1 month ago in milliseconds?

I want to calculate 1 month ago from current time in milliseconds. For example if date is 25.11.2016 then I want to get 25.10.2016 as milliseconds format. How can I calculate this date as milliseconds? Below code is not working properly I think.

System.currentTimeMillis() - 1000*60*60*24*30

Answer

You can use this way:

Calendar c = Calendar.getInstance(ZimeZone.getTimeZone("UTC"));
c.add(Calendar.MONTH, -1);
long result = c.getTimeInMillis();

Note that System.currentTimeMillis() is using UTC, so you need to create instance of Calendar using same TimeZone.