diff --git a/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java b/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java index 5a94b570cb6527afcf65297bf84ec0a93e124937..def38e3d80fb9b76a4dabe75de3079b142a6f1fb 100644 --- a/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java +++ b/src/main/java/com/kwan/springbootkwan/constant/CommonConstant.java @@ -17,4 +17,12 @@ public class CommonConstant { * redis前缀 */ public static final String PREFIX_REDIS_KEY = "test:"; + /** + * 7天,一周 + */ + public static final Integer SEVEN = 7; + /** + * 3天 + */ + public static final Integer THREE = 3; } diff --git a/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java b/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java index a435fa608d8b22be3f1e3e9c3d306983e17c999f..7eb9c9f2b42ea15e106ae0df496835b9669b73bb 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java +++ b/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java @@ -2,7 +2,13 @@ package com.kwan.springbootkwan.utils; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.StrUtil; +import com.kwan.springbootkwan.constant.CommonConstant; +import lombok.extern.slf4j.Slf4j; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * 时间工具类 @@ -11,17 +17,98 @@ import cn.hutool.core.util.StrUtil; * @version : 2.2.0 * @date : 2023/7/21 23:29 */ + +@Slf4j public class DateUtils { - public static void main(String[] args) { + + /** + * 获取前7天日期 + */ + public static String inMonth() { // 获取当前日期时间 DateTime now = DateUtil.date(); - // 获取当前日期所在的周数 - int weekOfYear = DateUtil.weekOfYear(now); // 获取当前日期所在的月份,并格式化为两位数字 - String month = String.format("%02d", DateUtil.month(now) + 1); - // 将周数和月份拼接成字符串 - String result = StrUtil.format("当前日期所在的周数:{},当前日期所在的月份:{}", weekOfYear, month); - System.out.println(result); + return String.format("%02d", DateUtil.month(now) + 1); + } + + /** + * 获取前7天日期 + */ + public static int inWeek() { + // 获取当前日期时间 + DateTime now = DateUtil.date(); + // 获取当前日期所在的周数 + return DateUtil.weekOfYear(now); + } + + /** + * 获取指定天的3天数据 + * + * @param date + * @return + */ + public static List before3Days(String date) { + List dates = new ArrayList<>(); + dates.add(date); + for (int i = 1; i < CommonConstant.THREE; i++) { + final DateTime dateTime = DateUtil.offsetDay(str2Date(date), -i); + dates.add(DateUtil.formatDate(dateTime)); + } + return dates; + } + + /** + * 获取前7天日期 + */ + public static List before7Days() { + List dates = new ArrayList<>(); + final DateTime yesterdayDate = DateUtil.yesterday(); + final String yesterday = DateUtil.formatDate(yesterdayDate); + dates.add(yesterday); + for (int i = 1; i < CommonConstant.SEVEN; i++) { + final DateTime dateTime = DateUtil.offsetDay(yesterdayDate, -i); + dates.add(DateUtil.formatDate(dateTime)); + } + return dates; + } + + /** + * 获取日期的集合 + * + * @param start + * @param end + * @return + */ + public static List getDateRangeList(String start, String end) { + LocalDate startDate = LocalDate.parse(start); + LocalDate endDate = LocalDate.parse(end); + List dateList = new ArrayList<>(); + LocalDate currentDate = startDate; + while (!currentDate.isAfter(endDate)) { + dateList.add(currentDate); + currentDate = currentDate.plusDays(1); + } + return dateList; + } + + + /** + * 字符串转日期 + * + * @param str + * @return + */ + public static Date str2Date(String str) { + return DateUtil.parse(str); + } + /** + * 日期转字符串 + * + * @param date + * @return + */ + public static String date2Str(Date date) { + return DateUtil.format(date, "yyyy-MM-dd"); } -} +} \ No newline at end of file