JAVA 获取时间段内的每一天

 1 public class day {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         String begintTime = "2019-03-26";
 6         String endTime =  "2019-03-04";
 7         for(String days: findDaysStr(begintTime,endTime)){
 8             System.out.println(days);
 9         }
10     }
11  
12     public static List<String> findDaysStr(String begintTime, String endTime) {
13         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
14         Date dBegin = null;
15         Date dEnd = null;
16         try {
17             dBegin = sdf.parse(begintTime);
18             dEnd = sdf.parse(endTime);
19         } catch (ParseException e) {
20             e.printStackTrace();
21         }
22         List<String> daysStrList = new ArrayList<String>();
23         daysStrList.add(sdf.format(dBegin));
24         Calendar calBegin = Calendar.getInstance();
25         calBegin.setTime(dBegin);
26         Calendar calEnd = Calendar.getInstance();
27         calEnd.setTime(dEnd);
28         while (dEnd.after(calBegin.getTime())) {
29             calBegin.add(Calendar.DAY_OF_MONTH, 1);
30             String dayStr = sdf.format(calBegin.getTime());
31             daysStrList.add(dayStr);
32         }
33         return daysStrList;
34     }
35 }
原文地址:https://www.cnblogs.com/yazoon/p/10495589.html