当前位置: 首页 > news >正文

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

背景

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

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);}
}

相关文章:

  • 通过金融科技(Fintech)掌控财务:智能理财管理指南
  • Design Compiler:转换时间过渡调整
  • 加油站小程序实战教程11会员注册
  • LeetCode面试热题150中19-22题学习笔记(用Java语言描述)
  • 链表知识回顾
  • windows 11 安装 redis
  • 基于Spring MVC的客户端真实IP获取方案解析
  • leetcode14.最长公共前缀
  • 微服务3--服务容错
  • Spring分析-IOC
  • 软件测试|App测试面试相关问题(2)
  • WPF 从Main()方法启动
  • 0-GLOP:学习全局划分和局部构造实时求解大规模路由问题(AAAI-24)
  • Sentinel源码—3.ProcessorSlot的执行过程二
  • JavaScript:表单及正则表达式验证
  • 鸿蒙NEXT开发Want工具类(ArkTs)
  • 批量将文件或文件夹复制分发到多个不同的文件夹
  • 【从零实现高并发内存池】Page Cache 从理解设计到全面实现
  • ArkTS组件的三个通用(通用事件、通用属性、通用手势)
  • 【版本控制】idea中使用git
  • 如何打开建设网站后台/如何网络媒体推广
  • 汕头市营商环境建设监督局网站/企业网站怎么推广
  • 建网站开发语言对比/优秀网站
  • 去菲律宾做it网站开发/seo工作室
  • 网站建设呼和浩特/如何进行网站的推广
  • 沈阳网站外包公司/十种营销方式