I recommend you switch from the outdated and error-prone java.util
date-time API to the modern java.time
date-time API. Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.
Do it as follows using the Java modern date-time API:
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getPastDaysOnIntervalOf(10, 5));// 10 days in past at an interval of 5 days
System.out.println(getPastDaysOnIntervalOf(5, 10));// 5 days in past at an interval of 10 days
}
static List<LocalDate> getPastDaysOnIntervalOf(int times, int interval) {
// The list to be populated with the desired dates
List<LocalDate> list = new ArrayList<>();
// Today
LocalDate date = LocalDate.now();
for (int i = 1; i <= times; i++) {
list.add(date);
date = date.minusDays(interval);
}
// Return the populated list
return list;
}
}
Output:
[2020-09-14, 2020-09-09, 2020-09-04, 2020-08-30, 2020-08-25, 2020-08-20, 2020-08-15, 2020-08-10, 2020-08-05, 2020-07-31]
[2020-09-14, 2020-09-04, 2020-08-25, 2020-08-15, 2020-08-05]
Using legacy API:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getPastDaysOnIntervalOf(10, 5));// 10 days in past at an interval of 5 days
System.out.println(getPastDaysOnIntervalOf(5, 10));// 5 days in past at an interval of 10 days
}
static List<Date> getPastDaysOnIntervalOf(int times, int interval) {
// The list to be populated with the desired dates
List<Date> list = new ArrayList<>();
// Today
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
for (int i = 1; i <= times; i++) {
list.add(date);
cal.add(Calendar.DATE, -interval);
date = cal.getTime();
}
// Return the populated list
return list;
}
}