JAVA 獲取兩個日期間的所有日期
阿新 • • 發佈:2018-12-03
public static List<String> getDates(Date startDate, Date endDate){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
List<String> dates = new ArrayList<>();
dates.add(sdf.format(startDate));
dates.add(sdf.format(endDate));
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(startDate);
tempStart.add(Calendar.DAY_OF_MONTH, 1);
//判斷是否到結束日期
while(tempStart.getTime().before(endDate)){
dates.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_MONTH, 1);
}
//日期去重
Set<String> result = new HashSet<>(dates);
return new ArrayList<>(result) ;
}