/**
*将日期段分割成单天
* @param beginDate
* @param endDate
* @return
* @throws ParseException
*/
public static List<String> splitDate(String beginDate ,String endDate) throws ParseException {
//1.先把String类型的日期转换为Date
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date begin = sf.parse(beginDate);
Date end = sf.parse(endDate);
//2.用日历类来实现时间比较
Calendar beginCal = Calendar.getInstance();//开始时间
Calendar endCal = Calendar.getInstance();//结束时间
//设置值
beginCal.setTime(begin);
endCal.setTime(end);
//实例化一个日期集合来接受跨天所有的日期数据
List<String> returnList = new ArrayList<String>();
//循环比较
while (beginCal.compareTo(endCal) < 1) {
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
returnList.add(sf1.format(beginCal.getTime()));
beginCal.add(beginCal.DATE, 1);//每次循环开始日期增加一天
}
return returnList;
}实验该方法********************************************************************************************************************
@GetMapping("/split/{start}/{end}")
public List split(@PathVariable String start ,@PathVariable String end){
List<String> datas = null;
try {
datas = splitDate(start,end);
} catch (ParseException e) {
e.printStackTrace();
}
return datas;
}调用示例 /split/2017-12-01/2018-01-01
结果集
[ "2017-12-01", "2017-12-02", "2017-12-03", "2017-12-04", "2017-12-05", "2017-12-06", "2017-12-07", "2017-12-08", "2017-12-09", "2017-12-10", "2017-12-11", "2017-12-12", "2017-12-13", "2017-12-14", "2017-12-15", "2017-12-16", "2017-12-17", "2017-12-18", "2017-12-19", "2017-12-20", "2017-12-21", "2017-12-22", "2017-12-23", "2017-12-24", "2017-12-25", "2017-12-26", "2017-12-27", "2017-12-28", "2017-12-29", "2017-12-30", "2017-12-31", "2018-01-01" ]