|
@@ -287,11 +287,16 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
* @return 包含所有日期的集合
|
|
* @return 包含所有日期的集合
|
|
* @throws ParseException 如果日期字符串格式不正确
|
|
* @throws ParseException 如果日期字符串格式不正确
|
|
*/
|
|
*/
|
|
- public static List<String> getAllDatesInRangeStr(String startDateStr, String endDateStr) throws ParseException {
|
|
|
|
|
|
+ public static List<String> getAllDatesInRangeStr(String startDateStr, String endDateStr) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
- Date startDate = sdf.parse(startDateStr);
|
|
|
|
- Date endDate = sdf.parse(endDateStr);
|
|
|
|
-
|
|
|
|
|
|
+ Date startDate = null;
|
|
|
|
+ Date endDate = null;
|
|
|
|
+ try {
|
|
|
|
+ startDate = sdf.parse(startDateStr);
|
|
|
|
+ endDate = sdf.parse(endDateStr);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
List<String> dateList = new ArrayList<>();
|
|
List<String> dateList = new ArrayList<>();
|
|
Calendar calendar = Calendar.getInstance();
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(startDate);
|
|
calendar.setTime(startDate);
|
|
@@ -300,7 +305,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
|
|
|
|
// 循环遍历日期范围
|
|
// 循环遍历日期范围
|
|
- while (!calendar.getTime().after(endDate)) {
|
|
|
|
|
|
+ while (calendar.getTime().before(endDate)) {
|
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
}
|
|
}
|
|
@@ -318,7 +323,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
|
|
|
|
// 循环遍历日期范围
|
|
// 循环遍历日期范围
|
|
- while (!calendar.getTime().after(endDate)) {
|
|
|
|
|
|
+ while (calendar.getTime().before(endDate)) {
|
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
dateList.add(sdf.format(calendar.getTime()));
|
|
}
|
|
}
|
|
@@ -332,9 +337,24 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
Date date1 = plusDate(-1, nowDate);
|
|
Date date1 = plusDate(-1, nowDate);
|
|
System.out.println(date);
|
|
System.out.println(date);
|
|
System.out.println(date1);
|
|
System.out.println(date1);
|
|
- System.out.println(getMonthsBefore("2024-02", 2));
|
|
|
|
|
|
+ /*System.out.println(getMonthsBefore("2024-02", 2));
|
|
System.out.println();
|
|
System.out.println();
|
|
System.out.println(getNYearSameDay("2024-02-01", 1));
|
|
System.out.println(getNYearSameDay("2024-02-01", 1));
|
|
- System.out.println(getNYearSameMonth("2024-02", 1));
|
|
|
|
|
|
+ System.out.println(getNYearSameMonth("2024-02", 1));*/
|
|
|
|
+ List<String> allDatesInRangeDate = getAllDatesInRangeDate(date1, date);
|
|
|
|
+ for (String s : allDatesInRangeDate) {
|
|
|
|
+ System.out.println(s);
|
|
|
|
+ }
|
|
|
|
+ System.out.println("------");
|
|
|
|
+ try {
|
|
|
|
+ String startDate = "2023-10-01";
|
|
|
|
+ String endDate = "2023-10-10";
|
|
|
|
+ List<String> dates = getAllDatesInRangeStr(startDate, endDate);
|
|
|
|
+ for (String s : dates) {
|
|
|
|
+ System.out.println(s);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|