两个时间:start="2019年11月13日" end="2019年12月05日"
/***
* <p>Title: getBetweenDates2</p>
* <p>Description: 获取两个时间之间的所有日期</p>
* @param start
* @param end
* @return
*/
public static List<String> getBetweenDates2(String start, String end) {
List<String> result = new ArrayList<String>();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
Date start_date = sdf.parse(start);
Date end_date = sdf.parse(end);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
//Collections.reverse(result);
return result;
}
测试方法
public static void main(String[] args) throws IOException, ParseException {
List<String> betweenDates = getBetweenDates2("2019年11月13日","2019年12月05日");
System.out.println(JSONObject.toJSONString(betweenDates));
}