獲取一段時(shí)間的列表 如3/29 - 4/19 中間20天的時(shí)間 此方法不會(huì)出現(xiàn)日期錯(cuò)亂的情況?
/**?
* 獲取一段時(shí)間的列表?
*/?
public static List getPeroidTime () {?
List time = new ArrayList<>(); // 返回的集合?
int lenght = 20; // 獲取天數(shù)的長(zhǎng)度?
int MDAY = 0; // 天數(shù)標(biāo)志?
String tday =”“; // 返回時(shí)間字符串?
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);?
Calendar c = Calendar.getInstance(); // 當(dāng)時(shí)的日期和時(shí)間?
int year = c.get(Calendar.YEAR); // 當(dāng)前年份?
int month = c.get(Calendar.MONTH)+1 ;// 獲取當(dāng)前月份?
int day = c.get(Calendar.DAY_OF_MONTH);//當(dāng)前的天數(shù)?
int maximum = c.getActualMaximum(Calendar.DAY_OF_MONTH);// 當(dāng)月最大天數(shù)?
// 獲取20的范圍 要判斷今天和當(dāng)月最大天數(shù)?
for (int i = 0; i < lenght; i++) {?
MDAY = day + i;?
if (maximum-MDAY>=0){ //當(dāng)月還有l(wèi)enght天?
c.set(Calendar.DAY_OF_MONTH, MDAY);?
tday = df.format(c.getTime());?
}else { //當(dāng)月沒(méi)有l(wèi)enght天?
c.set(Calendar.YEAR,year);?
c.set(Calendar.MONTH,month-1);// 設(shè)置月份 防止時(shí)間錯(cuò)亂?
c.set(Calendar.DAY_OF_MONTH, day+i);?
tday = df.format(c.getTime());?
}?
time.add(tday);?
}?
return time;?