From c42f3477a4f62e0428560e8775212ca16354ae19 Mon Sep 17 00:00:00 2001 From: LXW <1264174498@qq.com> Date: Tue, 28 May 2024 18:28:23 +0800 Subject: [PATCH] =?UTF-8?q?(improvement)(Headless)=20Add=20try=20catch=20t?= =?UTF-8?q?o=20avoid=20date=20'2024=E5=B9=B4'/'6=E6=9C=88'=20parse=20faile?= =?UTF-8?q?d=20causing=20the=20entire=20process=20to=20fail=20(#1040)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: jolunoluo --- .../supersonic/common/util/DateUtils.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java b/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java index 5d62163a8..6e828a69f 100644 --- a/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java +++ b/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java @@ -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 getDateList(String startDateStr, String endDateStr, String period) { - LocalDate startDate = LocalDate.parse(startDateStr); - LocalDate endDate = LocalDate.parse(endDateStr); - - List 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 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) {