I have a date of type java.util.Date
I want to subtract three months from it.
Not finding a lot of joy in the API.
public static Date getDateMonthsAgo(int numOfMonthsAgo)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -1 * numOfMonthsAgo);
return c.getTime();
}
will return the date X months in the past. Similarily, here's a function that returns the date X days in the past.
public static Date getDateDaysAgo(int numOfDaysAgo)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_YEAR, -1 * numOfDaysAgo);
return c.getTime();
}