Commit cafa938ff4d4e619a911b3276af4bf713456ef85
1 parent
e7876b15
common:LocalDateTimeUtils 增加方法:月份开始时间、月份结束时间
Showing
2 changed files
with
30 additions
and
14 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java
| ... | ... | @@ -6,6 +6,7 @@ import java.time.Duration; |
| 6 | 6 | import java.time.LocalDate; |
| 7 | 7 | import java.time.LocalDateTime; |
| 8 | 8 | import java.time.LocalTime; |
| 9 | +import java.time.temporal.TemporalAdjusters; | |
| 9 | 10 | |
| 10 | 11 | /** |
| 11 | 12 | * 时间工具类,用于 {@link java.time.LocalDateTime} |
| ... | ... | @@ -97,4 +98,29 @@ public class LocalDateTimeUtils { |
| 97 | 98 | LocalDateTime.of(nowDate, startTime2), LocalDateTime.of(nowDate, endTime2)); |
| 98 | 99 | } |
| 99 | 100 | |
| 101 | + /** | |
| 102 | + * 获取指定日期所在的月份的开始时间 | |
| 103 | + * 例如:2023-09-30 00:00:00,000 | |
| 104 | + * | |
| 105 | + * @param date 日期 | |
| 106 | + * @return 月份的开始时间 | |
| 107 | + */ | |
| 108 | + public static LocalDateTime beginOfMonth(LocalDateTime date) { | |
| 109 | + return date | |
| 110 | + .with(TemporalAdjusters.firstDayOfMonth()) | |
| 111 | + .with(LocalTime.MIN); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * 获取指定日期所在的月份的最后时间 | |
| 116 | + * 例如:2023-09-30 23:59:59,999 | |
| 117 | + * | |
| 118 | + * @param date 日期 | |
| 119 | + * @return 月份的结束时间 | |
| 120 | + */ | |
| 121 | + public static LocalDateTime endOfMonth(LocalDateTime date) { | |
| 122 | + return date | |
| 123 | + .with(TemporalAdjusters.lastDayOfMonth()) | |
| 124 | + .with(LocalTime.MAX); | |
| 125 | + } | |
| 100 | 126 | } | ... | ... |
yudao-module-mall/yudao-module-statistics-biz/src/main/java/cn/iocoder/yudao/module/statistics/service/trade/TradeStatisticsServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.statistics.service.trade; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; | |
| 4 | 5 | import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeStatisticsComparisonRespVO; |
| 5 | 6 | import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeSummaryRespVO; |
| 6 | 7 | import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeTrendSummaryRespVO; |
| ... | ... | @@ -13,8 +14,6 @@ import org.springframework.validation.annotation.Validated; |
| 13 | 14 | import javax.annotation.Resource; |
| 14 | 15 | import java.time.Duration; |
| 15 | 16 | import java.time.LocalDateTime; |
| 16 | -import java.time.LocalTime; | |
| 17 | -import java.time.temporal.TemporalAdjusters; | |
| 18 | 17 | import java.util.List; |
| 19 | 18 | |
| 20 | 19 | /** |
| ... | ... | @@ -79,18 +78,9 @@ public class TradeStatisticsServiceImpl implements TradeStatisticsService { |
| 79 | 78 | * @return 交易数据 |
| 80 | 79 | */ |
| 81 | 80 | private TradeSummaryRespBO getTradeSummaryByMonths(int months) { |
| 82 | - // TODO @疯狂:可以在 LocalDateUtils 封装方法;获得月份的开始;以及结束两个方法;然后这里就可以直接调用了 | |
| 83 | - // 月份开始时间 | |
| 84 | - LocalDateTime beginOfMonth = LocalDateTime.now() | |
| 85 | - .plusMonths(months) | |
| 86 | - .with(TemporalAdjusters.firstDayOfMonth()) | |
| 87 | - .with(LocalTime.MIN); | |
| 88 | - // 月份截止时间 | |
| 89 | - LocalDateTime endOfToday = LocalDateTime.now() | |
| 90 | - .plusMonths(months) | |
| 91 | - .with(TemporalAdjusters.lastDayOfMonth()) | |
| 92 | - .with(LocalTime.MAX); | |
| 93 | - return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(beginOfMonth, endOfToday); | |
| 81 | + LocalDateTime monthDate = LocalDateTime.now().plusMonths(months); | |
| 82 | + return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween( | |
| 83 | + LocalDateTimeUtils.beginOfMonth(monthDate), LocalDateTimeUtils.endOfMonth(monthDate)); | |
| 94 | 84 | } |
| 95 | 85 | |
| 96 | 86 | } | ... | ... |