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

打印 上一主题 下一主题

主题 1803|帖子 1803|积分 5411

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
背景

因邮件数据过多,因此接纳了分表的方式来举行存储, 支持按照天、周、月来分表以下为计划的方法
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.temporal.TemporalAdjusters;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class IndexSuffixCalculator {
  7.     // 按天格式化日期的模式
  8.     private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  9.     // 按周格式化日期的模式
  10.     private static final DateTimeFormatter WEEK_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  11.     // 按月格式化日期的模式
  12.     private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
  13.     /**
  14.      * 根据时间范围和配置计算索引后缀
  15.      *
  16.      * @param startDate 开始日期
  17.      * @param endDate   结束日期
  18.      * @param mode      配置模式:day, week 或 month
  19.      * @return 索引后缀列表
  20.      */
  21.     public List<String> calculateIndexSuffixes(LocalDate startDate, LocalDate endDate, String mode) {
  22.         if ("day".equalsIgnoreCase(mode)) {
  23.             return calculateDailySuffixes(startDate, endDate);
  24.         } else if ("week".equalsIgnoreCase(mode)) {
  25.             return calculateWeeklySuffixes(startDate, endDate);
  26.         } else if ("month".equalsIgnoreCase(mode)) {
  27.             return calculateMonthlySuffixes(startDate, endDate);
  28.         } else {
  29.             throw new IllegalArgumentException("Unsupported mode: " + mode);
  30.         }
  31.     }
  32.     /**
  33.      * 按天计算索引后缀
  34.      *
  35.      * @param startDate 开始日期
  36.      * @param endDate   结束日期
  37.      * @return 按天的索引后缀列表
  38.      */
  39.     private List<String> calculateDailySuffixes(LocalDate startDate, LocalDate endDate) {
  40.         List<String> suffixes = new ArrayList<>();
  41.         LocalDate currentDate = startDate;
  42.         while (!currentDate.isAfter(endDate)) {
  43.             suffixes.add(currentDate.format(DAY_FORMATTER));
  44.             currentDate = currentDate.plusDays(1); // 移动到下一天
  45.         }
  46.         return suffixes;
  47.     }
  48.     /**
  49.      * 按周计算索引后缀
  50.      *
  51.      * @param startDate 开始日期
  52.      * @param endDate   结束日期
  53.      * @return 按周的索引后缀列表
  54.      */
  55.     private List<String> calculateWeeklySuffixes(LocalDate startDate, LocalDate endDate) {
  56.         List<String> suffixes = new ArrayList<>();
  57.         LocalDate currentDate = startDate.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
  58.         while (!currentDate.isAfter(endDate)) {
  59.             suffixes.add(currentDate.format(WEEK_FORMATTER));
  60.             currentDate = currentDate.plusWeeks(1); // 移动到下一周的周一
  61.         }
  62.         return suffixes;
  63.     }
  64.     /**
  65.      * 按月计算索引后缀
  66.      *
  67.      * @param startDate 开始日期
  68.      * @param endDate   结束日期
  69.      * @return 按月的索引后缀列表
  70.      */
  71.     private List<String> calculateMonthlySuffixes(LocalDate startDate, LocalDate endDate) {
  72.         List<String> suffixes = new ArrayList<>();
  73.         LocalDate currentDate = startDate.with(TemporalAdjusters.firstDayOfMonth());
  74.         while (!currentDate.isAfter(endDate)) {
  75.             suffixes.add(currentDate.format(MONTH_FORMATTER));
  76.             currentDate = currentDate.plusMonths(1); // 移动到下一个月的第一天
  77.         }
  78.         return suffixes;
  79.     }
  80.     public static void main(String[] args) {
  81.         // 测试用例
  82.         LocalDate startDate = LocalDate.of(2025, 4, 1);
  83.         LocalDate endDate = LocalDate.of(2025, 6, 30);
  84.         IndexSuffixCalculator calculator = new IndexSuffixCalculator();
  85.         // 按天计算
  86.         List<String> dailySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "day");
  87.         System.out.println("Daily Suffixes: " + dailySuffixes);
  88.         // 按周计算
  89.         List<String> weeklySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "week");
  90.         System.out.println("Weekly Suffixes: " + weeklySuffixes);
  91.         // 按月计算
  92.         List<String> monthlySuffixes = calculator.calculateIndexSuffixes(startDate, endDate, "month");
  93.         System.out.println("Monthly Suffixes: " + monthlySuffixes);
  94.     }
  95. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表