1、把从Controller传入的startDate、endDate通过SimpleDateFormatjiexie解析(String --> Date)为日期类型;
2、转成long型的毫秒数,判断异常
3、按时间升序,则将sDate赋值给Calendar子对象,降序反之
4、创建List,将eDate时间之前的时间一个个添加进去(格式化为String类型)
public static List<String> getPerDaysByStartAndEndDate(String startDate, String endDate) {
DateFormat format = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
try {
Date sDate = format.parse(startDate);
Date eDate = format.parse(endDate);
long start = sDate.getTime(); //获得毫秒数
long end = eDate.getTime();
if (start > end) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(sDate); //降序 setTime(eDate)
List<String> res = new ArrayList<String>();
while (end >= start) {
res.add(format.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1); //降序 add(Calender.DAY_OF_MONTH, -1)
start = calendar.getTimeInMillis(); //降序 end = calender.getTimeInMillis();
}
return res;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}