Commit 01b225a9 by zhouhaibao

[+]新增参数

1 parent a88a4061
......@@ -9,6 +9,10 @@ public class NextPeriodCalculator {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static void main(String[] args) {
System.out.println(calculateNextPeriod(2, "2025-01-31"));
}
/**
* 计算下一周期的开始和结束时间
*
......@@ -19,35 +23,25 @@ public class NextPeriodCalculator {
public static NextPeriodRange calculateNextPeriod(Integer periodType, String endDateStr) {
LocalDate referenceDate;
if (periodType == 3) {
// 自定义:以下单结束时间为基准
if (endDateStr == null || endDateStr.trim().isEmpty()) {
throw new IllegalArgumentException("自定义周期必须提供 endDate");
}
referenceDate = LocalDate.parse(endDateStr, FORMATTER);
if (endDateStr == null || endDateStr.trim().isEmpty()) {
referenceDate = LocalDate.now(); // 默认用当前时间
} else {
// 周/月:以当前日期为基准
referenceDate = LocalDate.now();
referenceDate = LocalDate.parse(endDateStr, FORMATTER);
}
LocalDate nextStart;
LocalDate nextEnd;
switch (periodType) {
case 1: // 周
// 当前周的周一
LocalDate currentMonday = referenceDate.with(DayOfWeek.MONDAY);
// 下周一
nextStart = currentMonday.plusWeeks(1);
// 下周日
nextEnd = nextStart.plusDays(6);
case 1: // 周:以下一周期周一到周日
LocalDate nextMonday = referenceDate.with(DayOfWeek.MONDAY).plusWeeks(1);
nextStart = nextMonday;
nextEnd = nextMonday.plusDays(6);
break;
case 2: // 月
// 下个月的第一天
case 2: // 月:下一个月的完整月
nextStart = referenceDate.plusMonths(1).withDayOfMonth(1);
// 下个月的最后一天
nextEnd = nextStart.plusMonths(1).minusDays(1);
nextEnd = nextStart.with(TemporalAdjusters.lastDayOfMonth());
break;
case 4: // 自定义:结束后的一周(7天)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!