How to add days and get timestamp in Android?

449 Views Asked by At

I want to add days like 30, 60, 90, etc to a date a timestamp like 1642599000000 and want to get the new timestamp. I am using the following code but not working properly. It only works for 30 days only.

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(initialTimestamp);
    Date date= c.getTime();

    Calendar cal = new GregorianCalendar();
    cal.setTime(date);  
    cal.add(Calendar.DAY_OF_MONTH, 60); //Adding 60 days to current timestamp

    long newTimestamp = cal.getTime().getTime(); //Required this
1

There are 1 best solutions below

0
snachmsm On

use c.add(Calendar.DATE, 60); instead DAY_OF_MONTH

DAY_OF_MONTH may be used for fetching number of day in current month (not year, not numer of all days fit into timestamp, which is huge and contains thousands of days)