(improvement)(Headless) Add try catch to avoid date '2024年'/'6月' parse failed causing the entire process to fail (#1040)

Co-authored-by: jolunoluo
This commit is contained in:
LXW
2024-05-28 18:28:23 +08:00
committed by GitHub
parent 5064708c56
commit c42f3477a4

View File

@@ -1,6 +1,8 @@
package com.tencent.supersonic.common.util;
import com.google.common.collect.Lists;
import com.tencent.supersonic.common.pojo.Constants;
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
@@ -16,7 +18,6 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DateUtils {
@@ -147,25 +148,29 @@ public class DateUtils {
}
public static List<String> getDateList(String startDateStr, String endDateStr, String period) {
LocalDate startDate = LocalDate.parse(startDateStr);
LocalDate endDate = LocalDate.parse(endDateStr);
List<String> datesInRange = new ArrayList<>();
LocalDate currentDate = startDate;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
while (!currentDate.isAfter(endDate)) {
if (Constants.MONTH.equals(period)) {
datesInRange.add(currentDate.format(formatter));
currentDate = currentDate.plusMonths(1);
} else if (Constants.WEEK.equals(period)) {
datesInRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
currentDate = currentDate.plusWeeks(1);
} else {
datesInRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
currentDate = currentDate.plusDays(1);
try {
LocalDate startDate = LocalDate.parse(startDateStr);
LocalDate endDate = LocalDate.parse(endDateStr);
List<String> datesInRange = new ArrayList<>();
LocalDate currentDate = startDate;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
while (!currentDate.isAfter(endDate)) {
if (Constants.MONTH.equals(period)) {
datesInRange.add(currentDate.format(formatter));
currentDate = currentDate.plusMonths(1);
} else if (Constants.WEEK.equals(period)) {
datesInRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
currentDate = currentDate.plusWeeks(1);
} else {
datesInRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
currentDate = currentDate.plusDays(1);
}
}
return datesInRange;
} catch (Exception e) {
log.info("parse date failed, startDate:{}, endDate:{}", startDateStr, endDateStr, e);
}
return datesInRange;
return Lists.newArrayList();
}
public static boolean isAnyDateString(String value) {