String startTime = null;
String endTime = null;
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//获取当前季度开始月
Month minMonth = localDate.getMonth().firstMonthOfQuarter();
//获取当前季度结束月
Month maxMonth = minMonth.plus(2);
switch (type){
case 2:
//获取上周
localDate = today.plusDays(-7);
LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
startTime = monday.format(formatter);
endTime = sunday.format(formatter);
break;
case 3:
//获取本月
LocalDateTime nowMonthMin = LocalDateTime.of(localDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
LocalDateTime nowMonthMax = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
startTime = nowMonthMin.format(formatter);
endTime = nowMonthMax.format(formatter);
break;
case 4:
//获取上月
LocalDate lastDate = localDate.plusMonths(-1);
LocalDateTime lastMonthMin = LocalDateTime.of(lastDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
LocalDateTime lastMonthMax = LocalDateTime.of(lastDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
startTime = lastMonthMin.format(formatter);
endTime = lastMonthMax.format(formatter);
break;
case 5:
//获取本季度
LocalDate startDate = LocalDate.of(localDate.getYear(),minMonth,1);
LocalDate endDate = LocalDate.of(localDate.getYear(),maxMonth,maxMonth.length(localDate.isLeapYear()));
startTime = startDate.format(formatter);
endTime = endDate.format(formatter);
break;
case 6:
//获取上季度
minMonth = minMonth.plus(-3);
maxMonth = maxMonth.plus(-3);
startDate = LocalDate.of(localDate.getYear(),minMonth,1);
endDate = LocalDate.of(localDate.getYear(),maxMonth,maxMonth.length(localDate.isLeapYear()));
startTime = startDate.format(formatter);
endTime = endDate.format(formatter);
break;
case 7:
//获取本年
startDate = localDate.with(TemporalAdjusters.firstDayOfYear());
endDate = localDate.with(TemporalAdjusters.lastDayOfYear());
startTime = startDate.format(formatter);
endTime = endDate.format(formatter);
break;
case 8:
//获取去年
startDate = localDate.with(TemporalAdjusters.firstDayOfYear()).plusYears(-1);
endDate = localDate.with(TemporalAdjusters.lastDayOfYear()).plusYears(-1);
startTime = startDate.format(formatter);
endTime = endDate.format(formatter);
break;
default:
//获取本周
LocalDate monday = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate sunday = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
startTime = monday.format(formatter);
endTime = sunday.format(formatter);
}
- List item