使用场景:报表需求经常需要自行构造时刻表…
/** 获得指定区间日期并解析 */
public static List<String> getFormDate(Date startDate, Date endDate, String pattern) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Set<String> list = new LinkedHashSet<>();
while (true) {
boolean after = endDate.getTime() >= startCalendar.getTime().getTime();
if (!after) {
break;
}
list.add(sdf.format(startCalendar.getTime()));
startCalendar.add(Calendar.DATE, 1);
}
return new ArrayList<>(list);
}