From 665ffec07abac9fa14e7613fe1c73922a537ff77 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期二, 06 五月 2025 16:00:19 +0800 Subject: [PATCH] art: 设备管理-看板接口-维修统计接口 --- lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 105 insertions(+), 4 deletions(-) diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java index f1f4f8f..fa4d9a8 100644 --- a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java @@ -5,10 +5,11 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.TimeZone; +import java.time.LocalDate; +import java.time.YearMonth; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.*; import org.jeecg.common.constant.SymbolConstant; import org.springframework.util.StringUtils; @@ -764,8 +765,108 @@ return dateFormat.format(date); } + /** + * Date 杞� LocalDate + * @param date + * @return + */ + public static LocalDate dateToLocalDate(Date date) { + return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + /** + * LocalDate 杞� Date + * @param localDate + * @return + */ + public static Date localDateToDate(LocalDate localDate) { + return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + } + /** + * 鑾峰彇鏈湀绗竴澶� + * + * @return + */ + public static LocalDate getFirstOfMonth() { + LocalDate localDate = LocalDate.now(); + return localDate.withDayOfMonth(1); + } + /** + * 鑾峰彇鏈湀绗竴澶� + * + * @return + */ + public static LocalDate getFirstOfMonth(LocalDate localDate) { + return localDate.withDayOfMonth(1); + } + /** + * 鑾峰彇涓や釜鏈堜唤涓棿鐨勬墍鏈夋湀浠斤紝鍖呭惈寮�濮嬪拰缁撴潫鏈堜唤 + * + * @param startMonth + * @param endMonth + * @return + */ + public static List<String> getMonthsBetween(String startMonth, String endMonth) { + List<String> months = new ArrayList<>(); + + // 瀹氫箟鏃ユ湡鏍煎紡 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); + + // 瑙f瀽璧峰鍜岀粨鏉熸湀浠� + YearMonth start = YearMonth.parse(startMonth, formatter); + YearMonth end = YearMonth.parse(endMonth, formatter); + + // 纭繚start <= end 濡傛灉涓嶆槸 鍒欒繘琛屼氦鎹� + if (start.isAfter(end)) { + YearMonth temp = start; + start = end; + end = temp; + } + + // 寰幆娣诲姞鏈堜唤 + YearMonth current = start; + while (!current.isAfter(end)) { + months.add(current.format(formatter)); + current = current.plusMonths(1); + } + + return months; + } + + /** + * 鑾峰彇涓や釜鏈堜唤涓棿鐨勬墍鏈夋湀浠斤紝鍖呭惈寮�濮嬪拰缁撴潫鏈堜唤 + * + * @param startMonth + * @param endMonth + * @return + */ + public static List<String> getMonthsBetween(LocalDate startMonth, LocalDate endMonth) { + List<String> months = new ArrayList<>(); + + // 瀹氫箟鏃ユ湡鏍煎紡 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); + + // 瑙f瀽璧峰鍜岀粨鏉熸湀浠� + YearMonth start = YearMonth.of(startMonth.getYear(), startMonth.getMonth()); + YearMonth end = YearMonth.of(endMonth.getYear(), endMonth.getMonth()); + + // 纭繚start <= end 濡傛灉涓嶆槸 鍒欒繘琛屼氦鎹� + if (start.isAfter(end)) { + YearMonth temp = start; + start = end; + end = temp; + } + + // 寰幆娣诲姞鏈堜唤 + YearMonth current = start; + while (!current.isAfter(end)) { + months.add(current.format(formatter)); + current = current.plusMonths(1); + } + + return months; + } } \ No newline at end of file -- Gitblit v1.9.3