1. 通过传入计划的开始时间 - 截止时间 , 自动计算出今日所在是第几周期, 并返回当前周期的开始时间的结束时间
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;public class ReportingPeriodCalculator {// 计算给定日期所在季度的第一天
private static LocalDate firstDayOfQuarter(LocalDate date) {int month = date.getMonthValue();int quarterStartMonth = (month - 1) / 3 * 3 + 1; // 计算出季度开始的月份(1、4、7、10)return date.withMonth(quarterStartMonth).with(TemporalAdjusters.firstDayOfMonth());
}static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");public static ReportingPeriodDTO calculateObject(String startDateStr, String endDateStr, String periodType) {LocalDate startDate = LocalDate.parse(startDateStr);LocalDate endDate = LocalDate.parse(endDateStr);LocalDate today = LocalDate.now();// 确保开始日期在结束日期之前或相等if (startDate.isAfter(endDate) || startDate.isAfter(today)) {throw new IllegalArgumentException("开始日期不正确");}// 确保今天日期在结束日期之前或相等if (today.isAfter(endDate)) {throw new IllegalArgumentException("已经超出截止日期");}ReportingPeriodDTO dto = new ReportingPeriodDTO();switch (periodType) {case "每日":dto.setCycle((int) ChronoUnit.DAYS.between(startDate, today) + 1);dto.setStartTime(today.format(formatter));dto.setEndTime(today.format(formatter));return dto;case "每周":// 计算从开始日期到今天的完整周数,然后检查今天是否是新的一周的开始LocalDate startOfWeek = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 假设星期一是一周的第一天LocalDate todayStartOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));// 往前找,最近的礼拜一LocalDate weekEnd = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));// 往后找,最近的礼拜天long weeks = ChronoUnit.WEEKS.between(startOfWeek, todayStartOfWeek);if (!startDate.isEqual(todayStartOfWeek)) {weeks++;}dto.setCycle((int) weeks);// 当前是第几周期dto.setStartTime(todayStartOfWeek.format(formatter));// 本周周一if(endDate.isAfter(weekEnd)) { // 结束如期在礼拜天之后, 本周期结束日期则为周日 , 如果在其之前, 本周期结束日期则为截止日期dto.setEndTime(weekEnd.format(formatter));}else{dto.setEndTime(endDate.format(formatter));}return dto;case "每月":Period period = Period.between(startDate.withDayOfMonth(1), today.withDayOfMonth(1));int cycle = period.getYears() * 12 + period.getMonths() + (today.getDayOfMonth() >= startDate.getDayOfMonth() ? 1 : 0);LocalDate monthEnd = today.with(TemporalAdjusters.lastDayOfMonth());// 本月最后一天dto.setCycle(cycle);// 当前是第几周期dto.setStartTime(today.withDayOfMonth(1).format(formatter));// 本月第一天if(endDate.isAfter(monthEnd)) { // 截止日期在本周期结束日期之后, 则返回本周期结束日期, 否则返回截止日期dto.setEndTime(monthEnd.format(formatter));}else{dto.setEndTime(endDate.format(formatter));}return dto;case "每季度":LocalDate firstDayOfTodayQuarter = firstDayOfQuarter(today);// 本月所在的季度第一天LocalDate quarterEnd = firstDayOfTodayQuarter.plusMonths(3).minusDays(1);//本月所在的季度第一天的三个月以后的前一天LocalDate firstDayOfStartDateQuarter = firstDayOfQuarter(startDate);// 开始时间所在的季度第一天// 计算today所在的季度与startDate所在的季度之间的偏移量int quartersBetween = (int) ((ChronoUnit.MONTHS.between(firstDayOfStartDateQuarter, firstDayOfTodayQuarter) + 3) / 3);dto.setCycle(quartersBetween);// 当前是第几周期dto.setStartTime(firstDayOfTodayQuarter.format(formatter));if(endDate.isAfter(quarterEnd)) { // 截止日期在本周期结束日期之后, 则返回本周期结束日期, 否则返回截止日期dto.setEndTime(quarterEnd.format(formatter));}else{dto.setEndTime(endDate.format(formatter));}return dto;case "每半年":// 判断startDate和today是否在同一年的同一半年if (startDate.getYear() == today.getYear()) {if ((startDate.getMonthValue() < 7 && today.getMonthValue() < 7) || (startDate.getMonthValue() > 6 && today.getMonthValue() > 6)) {dto.setCycle(1);dto.setStartTime(startDate.withMonth(1).withDayOfMonth(1).format(formatter));if (endDate.getMonthValue() > 6) {dto.setEndTime(startDate.withMonth(6).withDayOfMonth(30).format(formatter));}else{dto.setEndTime(startDate.withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));}return dto;}if (startDate.getMonthValue() < 7 && today.getMonthValue() > 6) {dto.setCycle(2);dto.setStartTime(startDate.withMonth(7).withDayOfMonth(1).format(formatter));dto.setEndTime(startDate.withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));if (endDate.getMonthValue() != 12 && endDate.getDayOfMonth() != 31) {dto.setEndTime(startDate.withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));}else{dto.setEndTime(startDate.withMonth(12).withDayOfMonth(31).format(formatter));}return dto;}} else {// 如果不在同一年的同一半年,计算跨越的半年数量// 首先计算年份差int yearsBetween = today.getYear() - startDate.getYear();int x = 0;if (startDate.getMonthValue() < 7 && today.getMonthValue() < 7) {x = 1;dto.setStartTime(today.withMonth(1).withDayOfMonth(1).format(formatter));if (endDate.getMonthValue() > 6) {dto.setEndTime(today.withMonth(6).withDayOfMonth(30).format(formatter));}else{dto.setEndTime(today.withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));}}if (startDate.getMonthValue() < 7 && today.getMonthValue() > 6) {x = 2;dto.setStartTime(today.withMonth(7).withDayOfMonth(1).format(formatter));if (endDate.getMonthValue() != 12 && endDate.getDayOfMonth() != 31) {dto.setEndTime(today.withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));}else{dto.setEndTime(today.withMonth(12).withDayOfMonth(31).format(formatter));}}int cycle1 = x + (yearsBetween * 2);dto.setCycle(cycle1);return dto;}case "每年":int cycle2 = today.getYear() - startDate.getYear() + (today.isAfter(startDate.withYear(today.getYear())) ? 1 : 0);dto.setCycle(cycle2);dto.setStartTime(startDate.withYear(startDate.getYear()).format(formatter));int todayYear = today.getYear();int startYear = startDate.getYear();int endYear = endDate.getYear();if (todayYear == startYear) {dto.setStartTime(startDate.withYear(today.getYear()).withMonth(startDate.getMonthValue()).withDayOfMonth(startDate.getDayOfMonth()).format(formatter));}else{dto.setStartTime(startDate.withYear(today.getYear()).withMonth(1).withDayOfMonth(1).format(formatter));}if (todayYear == endYear) {dto.setEndTime(startDate.withYear(today.getYear()).withMonth(endDate.getMonthValue()).withDayOfMonth(endDate.getDayOfMonth()).format(formatter));}else{dto.setEndTime(startDate.withYear(today.getYear()).withMonth(12).withDayOfMonth(31).format(formatter));}return dto;default:throw new IllegalArgumentException("不支持的周期类型:" + periodType);}}public static void main(String[] args) {String startDate = "2023-01-01";String endDate = "2025-08-28";String periodType = "每年";calculateObject(startDate, endDate, periodType);System.out.println(calculateObject(startDate, endDate, periodType));// 每周 ReportingPeriodDTO{cycle=61, startTime='2024-02-19 00:00:00', endTime='2024-02-25 23:59:59'}// 每月 ReportingPeriodDTO{cycle=14, startTime='2024-02-01 00:00:00', endTime='2024-02-29 23:59:59'}// 每季度 ReportingPeriodDTO{cycle=2, startTime='2024-01-01 00:00:00', endTime='2024-12-31 23:59:59'}// 每半年 ReportingPeriodDTO{cycle=3, startTime='2024-01-01 00:00:00', endTime='2024-06-30 23:59:59'}// 每年 ReportingPeriodDTO{cycle=2, startTime='2024-01-01 00:00:00', endTime='2024-12-31 23:59:59'}}
}public class ReportingPeriodDTO {private Integer cycle;private String startTime;private String endTime;public Integer getCycle() {return cycle == null ? 0 : cycle;}public void setCycle(Integer cycle) {this.cycle = cycle;}public String getStartTime() {return startTime == null ? "" : startTime.trim();}public void setStartTime(String startTime) {this.startTime = startTime + " 00:00:00";}public String getEndTime() {return endTime == null ? "" : endTime.trim();}public void setEndTime(String endTime) {this.endTime = endTime + " 23:59:59";}@Overridepublic String toString() {return "ReportingPeriodDTO{" +"cycle=" + cycle +", startTime='" + startTime + '\'' +", endTime='" + endTime + '\'' +'}';}
}