common:LocalDateTimeUtils 增加方法:月份开始时间、月份结束时间

This commit is contained in:
owen 2023-09-30 22:21:47 +08:00
parent e7876b1520
commit cafa938ff4
2 changed files with 30 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
/** /**
* 时间工具类用于 {@link java.time.LocalDateTime} * 时间工具类用于 {@link java.time.LocalDateTime}
@ -97,4 +98,29 @@ public class LocalDateTimeUtils {
LocalDateTime.of(nowDate, startTime2), LocalDateTime.of(nowDate, endTime2)); LocalDateTime.of(nowDate, startTime2), LocalDateTime.of(nowDate, endTime2));
} }
/**
* 获取指定日期所在的月份的开始时间
* 例如2023-09-30 00:00:00,000
*
* @param date 日期
* @return 月份的开始时间
*/
public static LocalDateTime beginOfMonth(LocalDateTime date) {
return date
.with(TemporalAdjusters.firstDayOfMonth())
.with(LocalTime.MIN);
}
/**
* 获取指定日期所在的月份的最后时间
* 例如2023-09-30 23:59:59,999
*
* @param date 日期
* @return 月份的结束时间
*/
public static LocalDateTime endOfMonth(LocalDateTime date) {
return date
.with(TemporalAdjusters.lastDayOfMonth())
.with(LocalTime.MAX);
}
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.statistics.service.trade; package cn.iocoder.yudao.module.statistics.service.trade;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeStatisticsComparisonRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeStatisticsComparisonRespVO;
import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeSummaryRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeSummaryRespVO;
import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeTrendSummaryRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeTrendSummaryRespVO;
@ -13,8 +14,6 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List; import java.util.List;
/** /**
@ -79,18 +78,9 @@ public class TradeStatisticsServiceImpl implements TradeStatisticsService {
* @return 交易数据 * @return 交易数据
*/ */
private TradeSummaryRespBO getTradeSummaryByMonths(int months) { private TradeSummaryRespBO getTradeSummaryByMonths(int months) {
// TODO @疯狂可以在 LocalDateUtils 封装方法获得月份的开始以及结束两个方法然后这里就可以直接调用了 LocalDateTime monthDate = LocalDateTime.now().plusMonths(months);
// 月份开始时间 return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(
LocalDateTime beginOfMonth = LocalDateTime.now() LocalDateTimeUtils.beginOfMonth(monthDate), LocalDateTimeUtils.endOfMonth(monthDate));
.plusMonths(months)
.with(TemporalAdjusters.firstDayOfMonth())
.with(LocalTime.MIN);
// 月份截止时间
LocalDateTime endOfToday = LocalDateTime.now()
.plusMonths(months)
.with(TemporalAdjusters.lastDayOfMonth())
.with(LocalTime.MAX);
return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(beginOfMonth, endOfToday);
} }
} }