From ca64331ddc693fbf0d4c7cbefe2cbac8f88e10b9 Mon Sep 17 00:00:00 2001 From: zhuxinru Date: Mon, 17 Nov 2025 11:34:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E9=A1=B9=E7=9B=AEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sdm/common/utils/DateUtils.java | 232 ++++++++++++++++++ .../project/service/impl/NodeServiceImpl.java | 15 +- 2 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/com/sdm/common/utils/DateUtils.java diff --git a/common/src/main/java/com/sdm/common/utils/DateUtils.java b/common/src/main/java/com/sdm/common/utils/DateUtils.java new file mode 100644 index 00000000..2d21ed67 --- /dev/null +++ b/common/src/main/java/com/sdm/common/utils/DateUtils.java @@ -0,0 +1,232 @@ +package com.sdm.common.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAdjusters; +import java.util.Calendar; +import java.util.Date; + +/** + * 日期时间工具类 + */ +public class DateUtils { + + // 常用日期时间格式 + public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss"; + public static final String PATTERN_DATE = "yyyy-MM-dd"; + public static final String PATTERN_TIME = "HH:mm:ss"; + public static final String PATTERN_SLASH = "yyyy/MM/dd"; + public static final String PATTERN_CHINESE = "yyyy年MM月dd日"; + public static final String PATTERN_COMPACT = "yyyyMMddHHmmss"; + public static final String PATTERN_COMPACT_DATE = "yyyyMMdd"; + + private DateUtils() { + throw new IllegalStateException("Utility class"); + } + + /** + * 获取当前日期时间字符串 + */ + public static String getCurrentDateTime() { + return LocalDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN_DEFAULT)); + } + + /** + * 日期转字符串 + */ + public static String format(Date date, String pattern) { + if (date == null) { + return null; + } + SimpleDateFormat sdf = new SimpleDateFormat(pattern); + return sdf.format(date); + } + + /** + * 日期转字符串(默认格式) + */ + public static String format(Date date) { + return format(date, PATTERN_DEFAULT); + } + + /** + * 字符串转日期 + */ + public static Date parse(String dateStr, String pattern) { + if (dateStr == null || dateStr.trim().isEmpty()) { + return null; + } + try { + SimpleDateFormat sdf = new SimpleDateFormat(pattern); + return sdf.parse(dateStr); + } catch (ParseException e) { + throw new IllegalArgumentException("日期格式错误: " + dateStr, e); + } + } + + /** + * LocalDateTime 转 Date + */ + public static Date toDate(LocalDateTime localDateTime) { + if (localDateTime == null) { + return null; + } + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); + } + + /** + * LocalDate 转 Date + */ + public static Date toDate(LocalDate localDate) { + if (localDate == null) { + return null; + } + return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + } + + /** + * Date 转 LocalDateTime + */ + public static LocalDateTime toLocalDateTime(Date date) { + if (date == null) { + return null; + } + return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); + } + + /** + * Date 转 LocalDate + */ + public static LocalDate toLocalDate(Date date) { + if (date == null) { + return null; + } + return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + + /** + * 获取指定日期的开始时间(00:00:00) + */ + public static Date getStartOfDay(Date date) { + if (date == null) { + return null; + } + LocalDateTime localDateTime = toLocalDateTime(date) + .toLocalDate() + .atStartOfDay(); + return toDate(localDateTime); + } + + /** + * 获取指定日期的结束时间(23:59:59) + */ + public static Date getEndOfDay(Date date) { + if (date == null) { + return null; + } + LocalDateTime localDateTime = toLocalDateTime(date) + .toLocalDate() + .atTime(LocalTime.MAX); + return toDate(localDateTime); + } + + /** + * 日期加减(天数) + */ + public static Date addDays(Date date, int days) { + if (date == null) { + return null; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.add(Calendar.DAY_OF_MONTH, days); + return calendar.getTime(); + } + + /** + * 日期加减(月数) + */ + public static Date addMonths(Date date, int months) { + if (date == null) { + return null; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.add(Calendar.MONTH, months); + return calendar.getTime(); + } + + /** + * 日期加减(年数) + */ + public static Date addYears(Date date, int years) { + if (date == null) { + return null; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.add(Calendar.YEAR, years); + return calendar.getTime(); + } + + /** + * 获取两个日期之间的天数差 + */ + public static long daysBetween(Date startDate, Date endDate) { + if (startDate == null || endDate == null) { + throw new IllegalArgumentException("日期不能为空"); + } + LocalDate start = toLocalDate(startDate); + LocalDate end = toLocalDate(endDate); + return Math.abs(start.until(end).getDays()); + } + + /** + * 判断是否为同一天 + */ + public static boolean isSameDay(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return false; + } + LocalDate localDate1 = toLocalDate(date1); + LocalDate localDate2 = toLocalDate(date2); + return localDate1.isEqual(localDate2); + } + + /** + * 判断日期是否在指定范围内 + */ + public static boolean isBetween(Date date, Date startDate, Date endDate) { + if (date == null || startDate == null || endDate == null) { + return false; + } + return !date.before(startDate) && !date.after(endDate); + } + + /** + * 获取月份的第一天 + */ + public static Date getFirstDayOfMonth(Date date) { + if (date == null) { + return null; + } + LocalDate localDate = toLocalDate(date) + .with(TemporalAdjusters.firstDayOfMonth()); + return toDate(localDate); + } + + /** + * 获取月份的最后一天 + */ + public static Date getLastDayOfMonth(Date date) { + if (date == null) { + return null; + } + LocalDate localDate = toLocalDate(date) + .with(TemporalAdjusters.lastDayOfMonth()); + return toDate(localDate); + } + +} \ No newline at end of file diff --git a/project/src/main/java/com/sdm/project/service/impl/NodeServiceImpl.java b/project/src/main/java/com/sdm/project/service/impl/NodeServiceImpl.java index 5ef430d2..e33c1e7b 100644 --- a/project/src/main/java/com/sdm/project/service/impl/NodeServiceImpl.java +++ b/project/src/main/java/com/sdm/project/service/impl/NodeServiceImpl.java @@ -19,6 +19,7 @@ import com.sdm.common.entity.resp.system.SysUserGroupDetailResp; import com.sdm.common.feign.impl.data.DataClientFeignClientImpl; import com.sdm.common.feign.impl.system.SysUserFeignClientImpl; import com.sdm.common.feign.inter.data.IDataFeignClient; +import com.sdm.common.utils.DateUtils; import com.sdm.common.utils.RandomUtil; import com.sdm.project.dao.SimulationDemandMapper; import com.sdm.project.dao.SimulationNodeMapper; @@ -104,8 +105,11 @@ public class NodeServiceImpl extends ServiceImpl cidUserRespSdmResponse = sysUserFeignClient.queryUserDetail(UserQueryReq.builder().userId(creator).build()); + if (cidUserRespSdmResponse.isSuccess()) { + spdmNodeVo.setCreatorObj(cidUserRespSdmResponse.getData()); + } + } } JSONObject jsonObject = new JSONObject(); jsonObject.put("data", nodeList);