【Java】时间区间内按天、周、月份索引后缀计算方法

发布于:2025-04-20 ⋅ 阅读:(16) ⋅ 点赞:(0)
背景

因邮件数据过多,因此采用了分表的方式来进行存储, 支持按照天、周、月来分表以下为设计的方法

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class IndexSuffixCalculator {

    // 按天格式化日期的模式
    private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    // 按周格式化日期的模式
    private static final DateTimeFormatter WEEK_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    // 按月格式化日期的模式
    private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");

    /**
     * 根据时间范围和配置计算索引后缀
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @param mode      配置模式:day, week 或 month
     * @return 索引后缀列表
     */
    public List<String> calculateIndexSuffixes(LocalDate startDate, LocalDate endDate, String mode) {
        if ("day".equalsIgnoreCase(mode)) {
            return calculateDailySuffixes(startDate, endDate);
        } else if ("week".equalsIgnoreCase(mode)) {
            return calculateWeeklySuffixes(startDate, endDate);
        } else if ("month".equalsIgnoreCase(mode)) {
            return calculateMonthlySuffixes(startDate, endDate);
        } else {
            throw new IllegalArgumentException("Unsupported mode: " + mode);
        }
    }

    /**
     * 按天计算索引后缀
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 按天的索引后缀列表
     */
    private List<String> calculateDailySuffixes(LocalDate startDate, LocalDate endDate) {
        List<String> suffixes = new ArrayList<>();
        LocalDate currentDate = startDate;

        while (!currentDate.isAfter(endDate)) {
            suffixes.add(currentDate.format(DAY_FORMATTER));
            currentDate = currentDate.plusDays(1); // 移动到下一天
        }

        return suffixes;
    }

    /**
     * 按周计算索引后缀
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 按周的索引后缀列表
     */
    private List<String> calculateWeeklySuffixes(LocalDate startDate, LocalDate endDate) {
        List<String> suffixes = new ArrayList<>();
        LocalDate currentDate = startDate.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));

        while (!currentDate.isAfter(endDate)) {
            suffixes.add(currentDate.format(WEEK_FORMATTER));
            currentDate = currentDate.plusWeeks(1); // 移动到下一周的周一
        }

        return suffixes;
    }

    /**
     * 按月计算索引后缀
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 按月的索引后缀列表
     */
    private List<String> calculateMonthlySuffixes(LocalDate startDate, LocalDate endDate) {
        List<String> suffixes = new ArrayList<>();
        LocalDate currentDate = startDate.with(TemporalAdjusters.firstDayOfMonth());

        while (!currentDate.isAfter(endDate)) {
            suffixes.add(currentDate.format(MONTH_FORMATTER));
            currentDate = currentDate.plusMonths(1); // 移动到下一个月的第一天
        }

        return suffixes;
    }

    public static void main(String[] args) {
        // 测试用例
        LocalDate startDate = LocalDate.of(2025, 4, 1);
        LocalDate endDate = LocalDate.of(2025, 6, 30);

        IndexSuffixCalculator calculator = new IndexSuffixCalculator();

        // 按天计算
        List<String> dailySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "day");
        System.out.println("Daily Suffixes: " + dailySuffixes);

        // 按周计算
        List<String> weeklySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "week");
        System.out.println("Weekly Suffixes: " + weeklySuffixes);

        // 按月计算
        List<String> monthlySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "month");
        System.out.println("Monthly Suffixes: " + monthlySuffixes);
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到