java获取时间段内的所有日期
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateFirst="2017-05-01";
String dateSecond="2017-05-25";
try{
Date startDate = dateFormat.parse(dateFirst);
Date endDate = dateFormat.parse(dateSecond);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while(calendar.getTime().before(endDate)){
System.out.println(dateFormat.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(dateFormat.format(endDate));
}
catch(Exception e){
e.printStackTrace();
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateFirst="2017-05-01";
String dateSecond="2017-05-25";
try{
Date startDate = dateFormat.parse(dateFirst);
Date endDate = dateFormat.parse(dateSecond);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while(calendar.getTime().before(endDate)){
System.out.println(dateFormat.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(dateFormat.format(endDate));
}
catch(Exception e){
e.printStackTrace();
}
}