根据周期频率,按周、月、年、半年、季度生成各自频率至今的周期开始时间~结束时间
/**
* 获取搜索周期
*
* @param frequency 周期频率
* @param beginMonth 开始日期
* @param beginMonth 结束日期,取当前时间
* @return
*/
public static List<SearchCycle> getSearchCycle(String frequency, String beginMonth,String endMonth) {
LocalDate beginDate = DateUtils.asLocalDate(DateUtils.stringToDate(beginMonth, "yyyy-MM"));
List<SearchCycle> list = new ArrayList<SearchCycle>();
//获取当前日期
LocalDate currentDate = LocalDate.now();
if(null != endMonth){
currentDate = DateUtils.asLocalDate(DateUtils.stringToDate(endMonth, "yyyy-MM"));
}
if (currentDate != null && beginDate != null) {
if (currentDate.getYear() < beginDate.getYear()) {
throw new AppException("传入日期大于当前日期");
}
int subtractYear = currentDate.getYear() - beginDate.getYear();
if (YEAR.equals(frequency)) {
for (int i = 0; i <= subtractYear; i++) {
LocalDate startDate = LocalDate.ofYearDay(beginDate.getYear() + i, 1);
LocalDate endDate = DateUtils.yearEndDate(startDate);
list.add(getSearchCycle(startDate, endDate,frequency));
}
}
//半年
if (HALF_YEAR.equals(frequency)) {
int halfYearBeginYear = Integer.parseInt(beginMonth.substring(0, 4));
int halfYearBeginMonth = Integer.parseInt(beginMonth.split("-")[1]);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
if(halfYearBeginMonth >= 7 ){//下半年任务
min.setTime(sdf.parse(halfYearBeginYear+"-07-01"));
}else{
min.setTime(sdf.parse(halfYearBeginYear+"-01-01"));
}
} catch (ParseException e) {
log.error("补充任务半年度时间格式化失败",e);
}
while (min.before(max) || min.equals(max)) {
LocalDate localDate1 = min.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
Calendar endCalendar = Calendar.getInstance();
try {
endCalendar.setTime(sdf.parse(String.valueOf(localDate1)));
}catch(ParseException e){
log.error("补充任务半年度时间格式化失败",e);
}
endCalendar.add(Calendar.MONTH, 6);
// 减少一天;
endCalendar.add(Calendar.DATE, -1);
LocalDate endDate = endCalendar.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
list.add(getSearchCycle(localDate1, endDate,frequency));
min.add(Calendar.MONTH, 6);
}
}
//季度
if (QUARTER.equals(frequency)) {
int quarterBeginYear = Integer.parseInt(beginMonth.substring(0, 4));
int quarterBeginMonth = Integer.parseInt(beginMonth.split("-")[1]);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
List<String> quarter2 = Arrays.asList("4", "5", "6");
List<String> quarter3 = Arrays.asList("7", "8", "9");
List<String> quarter4 = Arrays.asList("10", "11", "12");
if(quarter2.contains(String.valueOf(quarterBeginMonth))){
min.setTime(sdf.parse(quarterBeginYear+"-04-01"));
}else if(quarter3.contains(String.valueOf(quarterBeginMonth))){
min.setTime(sdf.parse(quarterBeginYear+"-07-01"));
}else if(quarter4.contains(String.valueOf(quarterBeginMonth))){
min.setTime(sdf.parse(quarterBeginYear+"-10-01"));
}else{
min.setTime(sdf.parse(quarterBeginYear+"-01-01"));
}
} catch (ParseException e) {
log.error("补充任务半年度时间格式化失败",e);
}
while (min.before(max) || min.equals(max)) {
LocalDate localDate1 = min.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
Calendar endCalendar = Calendar.getInstance();
try {
endCalendar.setTime(sdf.parse(String.valueOf(localDate1)));
}catch(ParseException e){
log.error("补充任务半年度时间格式化失败",e);
}
endCalendar.add(Calendar.MONTH, 3);
// 减少一天;
endCalendar.add(Calendar.DATE, -1);
LocalDate endDate = endCalendar.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
list.add(getSearchCycle(localDate1, endDate,frequency));
min.add(Calendar.MONTH, 3);
}
}
//月度
if (MONTH.equals(frequency)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
min.setTime(sdf.parse(String.valueOf(DateUtils.monthStartDate(beginDate))));
max.setTime(sdf.parse(String.valueOf(currentDate)));
} catch (ParseException e) {
log.error("补充任务月度时间格式化失败",e);
}
while (min.before(max) || min.equals(max)) {
LocalDate startDate = min.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
Calendar endCalendar = Calendar.getInstance();
try {
endCalendar.setTime(sdf.parse(String.valueOf(startDate)));
}catch(ParseException e){
log.error("补充任务月度时间格式化失败",e);
}
endCalendar.add(Calendar.MONTH, 1);
// 减少一天;月度任务在月底结束
endCalendar.add(Calendar.DATE, -1);
LocalDate endDate = endCalendar.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
list.add(getSearchCycle(startDate, endDate,frequency));
min.add(Calendar.MONTH, 1);
}
}
if(WEEK.equals(frequency)){
getStageByWeek(beginMonth, list);
}
}
return list;
}
处理周度任务,周一为开始日期周日为结束日期
private static void getStageByWeek(String beginDate, List<SearchCycle> list) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try {
// 找到开始日期的周一
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate periodStartDate;
LocalDate localDateParse = LocalDate.parse(beginDate, formatter);
if(DayOfWeek.MONDAY.equals(localDateParse.getDayOfWeek())){//当前日期是周一
periodStartDate=localDateParse;
}else{
periodStartDate = localDateParse.with(ChronoField.DAY_OF_WEEK, 1);
}
min.setTime(sdf.parse(String.valueOf(periodStartDate)));
} catch (ParseException e) {
log.info("补充任务周度时间格式化失败");
}
while (min.before(max) || min.equals(max)) {
LocalDate startDate = min.getTime().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
WeekFields weekFields = WeekFields.of(Locale.getDefault());
//计算当前日期为第几周 作为期别
int currentWeek = startDate.get(weekFields.weekOfWeekBasedYear());
// 获取当前日期所在的年份(基于周年)
int currentYear = startDate.get(weekFields.weekBasedYear());
String stage =currentYear+"年第"+ currentWeek+"周";
LocalDate currentEndDate = startDate.plusDays(6);
list.add(new SearchCycle(startDate + "~" + currentEndDate, startDate, currentEndDate,stage));
min.add(Calendar.DATE, 7);
}
}