import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.time.temporal.ChronoUnit;
public class Test {
public static void main(String[] args) throws ParseException {
String startTime = "2024-11-28";
String endTime = "2024-12-02";
List<String> dateList = Lists.newArrayList();
dateList.add(startTime);
long days = betweenDays(startTime, endTime);
for (int i = 0; i < days; i++) {
Date tempDate = MyDateUtil.dateAdd(MyDateUtil.strToDate(startTime), 1);
startTime = MyDateUtil.dateToStr(tempDate);
dateList.add(startTime);
}
System.out.println(dateList.toString());
}
}
//打印输出:[2024-11-28, 2024-11-29, 2024-11-30, 2024-12-01, 2024-12-02]
public static long betweenDays(String date1, String date2) {LocalDate startDate = LocalDate.parse(date1);LocalDate endDate = LocalDate.parse(date2);return ChronoUnit.DAYS.between(startDate, endDate);
}
public static Date dateAdd(Date startTime, Integer dd) {if (startTime == null) {startTime = MyDateUtil.getNowTime();}Calendar cal = Calendar.getInstance();cal.setTime(startTime);if (dd != null && !dd.equals(0)) {cal.add(Calendar.DATE, dd);}return new Date(cal.getTime().getTime());
}
public static Date strToDate(String strDate) throws ParseException {return MyDateUtil.strToDate(MyDateUtil.Y4MD, strDate); }
public static final String Y4MD = "yyyy-MM-dd";
public static Date strToDate(String aMask, String strDate) throws ParseException {Date date = null;if (StringUtil.isEmpty(strDate)) {return date;}if (StringUtil.isEmpty(aMask)) {aMask = MyDateUtil.YMDHMS;}SimpleDateFormat df = new SimpleDateFormat(aMask);date = df.parse(strDate);return (date); }