diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java index 0909d3e582..c222c4ce2b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java @@ -571,6 +571,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe ScheduleParam scheduleParam = JSONUtils.parseObject(schedule, ScheduleParam.class); Date now = new Date(); + assert scheduleParam != null; Date startTime = DateUtils.transformTimezoneDate(scheduleParam.getStartTime(), scheduleParam.getTimezoneId()); Date endTime = DateUtils.transformTimezoneDate(scheduleParam.getEndTime(), scheduleParam.getTimezoneId()); startTime = now.after(startTime) ? now : startTime; diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java index 6ee9ac99d7..392140e555 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java @@ -159,6 +159,38 @@ public final class DateUtils { return format(date, YYYY_MM_DD_HH_MM_SS, timezone); } + /** + * convert zone date time to yyyy-MM-dd HH:mm:ss format + * + * @param zonedDateTime zone date time + * @return zone date time string + */ + public static String dateToString(ZonedDateTime zonedDateTime) { + return YYYY_MM_DD_HH_MM_SS.format(zonedDateTime); + } + + /** + * convert zone date time to yyyy-MM-dd HH:mm:ss format + * + * @param zonedDateTime zone date time + * @param timezone time zone + * @return zone date time string + */ + public static String dateToString(ZonedDateTime zonedDateTime, String timezone) { + return dateToString(zonedDateTime, ZoneId.of(timezone)); + } + + /** + * convert zone date time to yyyy-MM-dd HH:mm:ss format + * + * @param zonedDateTime zone date time + * @param zoneId zone id + * @return zone date time string + */ + public static String dateToString(ZonedDateTime zonedDateTime, ZoneId zoneId) { + return DateTimeFormatter.ofPattern(Constants.YYYY_MM_DD_HH_MM_SS).withZone(zoneId).format(zonedDateTime); + } + /** * convert string to date and time * diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java index 96c4450923..2042fa2d80 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java @@ -21,6 +21,8 @@ import org.apache.dolphinscheduler.common.thread.ThreadLocalContext; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Date; import java.util.TimeZone; @@ -245,4 +247,15 @@ public class DateUtilsTest { Assert.assertEquals(Timer.ONE_HOUR * 8, utcDate.getTime() - shanghaiDate.getTime()); } + + @Test + public void testDateToString() { + ZoneId asiaSh = ZoneId.of("Asia/Shanghai"); + ZoneId utc = ZoneId.of("UTC"); + ZonedDateTime asiaShNow = ZonedDateTime.now(asiaSh); + ZonedDateTime utcNow = asiaShNow.minusHours(8); + String asiaShNowStr = DateUtils.dateToString(utcNow, asiaSh); + String utcNowStr = DateUtils.dateToString(asiaShNow, utc); + Assert.assertEquals(asiaShNowStr, utcNowStr); + } }