效果
- 最近7天:2024年6月21日-2024年6月27日
- 过去一周、最近一周:2024年6月16日-2024年6月22日
- 过去三个月:2024年3月-2024年6月
- 近半年、过去半年:2023年12月-2024年6月
- 去年:2023年1月-2023年12月
- 过去3年:2021年1月-2024年12月
- 累计:2020年1月1日-2024年6月27日
代码
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Locale;public class DataFormatUtil {public static String getDates(String timeType) {LocalDate now = LocalDate.now();if ("日".equals(timeType)) {return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));}if ("周".equals(timeType)) {return now.format(DateTimeFormatter.ofPattern("yyyy-'W'ww"));}if ("月".equals(timeType)) {return now.format(DateTimeFormatter.ofPattern("yyyy-MM"));}if ("季".equals(timeType)) {String quarter = String.valueOf((now.getMonthValue() - 1) / 3 + 1);return now.format(DateTimeFormatter.ofPattern("yyyy")) + "-Q" + quarter;}if ("年".equals(timeType)) {return now.format(DateTimeFormatter.ofPattern("yyyy"));}return "未知时间颗粒度";}public static String getAllTimeRanges() {LocalDate now = LocalDate.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");String currentDateString = now.format(formatter);LocalDate currentDate = LocalDate.parse(currentDateString, DateTimeFormatter.BASIC_ISO_DATE);LocalDate startDate;String[] timeRanges = new String[8];startDate = currentDate.minusDays(6);timeRanges[0] = String.format("最近7天:%d年%d月%d日-%d年%d月%d日",startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth(),currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());startDate = currentDate.with(WeekFields.of(Locale.getDefault()).dayOfWeek(), 1).minusWeeks(1);LocalDate endDate = startDate.plusDays(6);timeRanges[1] = String.format("过去一周、最近一周:%d年%d月%d日-%d年%d月%d日",startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth(),endDate.getYear(), endDate.getMonthValue(), endDate.getDayOfMonth());startDate = currentDate.minusMonths(3);timeRanges[2] = String.format("过去三个月:%d年%d月-%d年%d月",startDate.getYear(), startDate.getMonthValue(),currentDate.getYear(), currentDate.getMonthValue());startDate = currentDate.minusMonths(6);timeRanges[3] = String.format("近半年、过去半年:%d年%d月-%d年%d月",startDate.getYear(), startDate.getMonthValue(),currentDate.getYear(), currentDate.getMonthValue());startDate = currentDate.minusYears(1).withMonth(1).withDayOfMonth(1);endDate = startDate.plusYears(1).minusDays(1);timeRanges[4] = String.format("去年:%d年1月-%d年12月",startDate.getYear(), startDate.getYear());startDate = currentDate.minusYears(3);timeRanges[5] = String.format("过去3年:%d年1月-%d年12月",startDate.getYear(), currentDate.getYear());startDate = LocalDate.of(2020, 1, 1);timeRanges[6] = String.format("累计:%d年1月1日-%d年%d月%d日",startDate.getYear(), currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());StringBuilder result = new StringBuilder();for (int i = 0; i < timeRanges.length - 1; i++) {result.append(i + 1).append(". ").append(timeRanges[i]).append("\n");}return result.toString();}
}