Commit 01b225a9 by zhouhaibao

[+]新增参数

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