android get a list of times

Get a list of a period of time, such as 3/29 - 4/19, the middle 20 days. This method will not cause date confusion
/**
* Get a list of a period of time
*/
public static List getPeroidTime () {
List time = new ArrayList <>(); // Returned set
int lenght = 20; // Get the length of days
int MDAY = 0; // Day flag
String tday = ""; // Return time string
SimpleDateFormat df = new SimpleDateFormat("yyyy -MM-dd”);
Calendar c = Calendar.getInstance(); // current date and time
int year = c.get(Calendar.YEAR); // current year
int month = c.get(Calendar.MONTH) +1 ;// Get the current month
int day = c.get(Calendar.DAY_OF_MONTH);//The current number of days
int maximum = c.getActualMaximum(Calendar.DAY_OF_MONTH);// The maximum number of days in the current month
// The range to get 20 needs to be judged Maximum days in today and month
for (int i = 0; i < lenght; i++) {
MDAY = day + i;
if (maximum-MDAY>=0){ //There are still lenght days in the month
c.set(Calendar.DAY_OF_MONTH, MDAY);
tday = df.format(c.getTime());
}else { //There are no lenght days in the month
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,month-1);// Set the month to prevent time confusion
c.set(Calendar.DAY_OF_MONTH, day+i);
tday = df.format( c.getTime());
}
time.add(tday);
}
return time;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594225&siteId=291194637