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

java时间处理工具类

效果

  1. 最近7天:2024年6月21日-2024年6月27日
  2. 过去一周、最近一周:2024年6月16日-2024年6月22日
  3. 过去三个月:2024年3月-2024年6月
  4. 近半年、过去半年:2023年12月-2024年6月
  5. 去年:2023年1月-2023年12月
  6. 过去3年:2021年1月-2024年12月
  7. 累计: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)) {// 格式化为 YYYY-MM-DDreturn now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));}if ("周".equals(timeType)) {// 格式化为 YYYY-WX// 周数(W)是按照ISO 8601标准,每周从周一开始,每年的第一周至少包含4天return now.format(DateTimeFormatter.ofPattern("yyyy-'W'ww"));}if ("月".equals(timeType)) {// 格式化为 YYYY-MMreturn now.format(DateTimeFormatter.ofPattern("yyyy-MM"));}if ("季".equals(timeType)) {// 格式化为 YYYY-QX// 季度(Q)是按照每个季度三个月计算,1-3月为第一季度,4-6月为第二季度,依此类推String quarter = String.valueOf((now.getMonthValue() - 1) / 3 + 1);return now.format(DateTimeFormatter.ofPattern("yyyy")) + "-Q" + quarter;}if ("年".equals(timeType)) {// 格式化为 YYYYreturn now.format(DateTimeFormatter.ofPattern("yyyy"));}return "未知时间颗粒度";}/*** 处理特殊时间** @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];// 最近7天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());// 过去3年startDate = currentDate.minusYears(3);timeRanges[5] = String.format("过去3年:%d年1月-%d年12月",startDate.getYear(), currentDate.getYear());// 累计,这里假设项目开始日期为2020年1月1日startDate = LocalDate.of(2020, 1, 1);timeRanges[6] = String.format("累计:%d年1月1日-%d年%d月%d日",startDate.getYear(), currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());// 为了简化,这里我们忽略"近XX月"、"近XX年"的情况,因为它们与"过去XX月"、"过去XX年"的处理相同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();}
}
http://www.lryc.cn/news/384954.html

相关文章:

  • Android高级面试_2_IPC相关
  • docker封禁对外端口映射
  • 【leetcode系列】567.字符串的排列(滑动窗口)
  • 情感分析方法与实践
  • 迁移学习——CycleGAN
  • 【软件测试】对于测试中的bug,我们真正了解了吗?
  • Packer-Fuzzer一款好用的前端高效安全扫描工具
  • 解决卸载TabX explorer软件后导致系统文件资源管理器无法正常使用问题
  • qt for android 使用打包sqlite数据库文件方法
  • MYBATIS大于等于、小于等于的写法
  • 基于堆叠长短期记忆网络 Stacked LSTM 预测A股股票价格走势
  • SpringCloud Alibaba Sentinel基础入门与安装
  • Arduino IDE下载、安装和配置
  • SOBEL图像边缘检测器的设计
  • Day35:2734. 执行字串操作后的字典序最小字符串
  • 【高考志愿】机械工程
  • ffmpeg将mp4转换为swf
  • 论文学习 --- RL Regret-based Defense in Adversarial Reinforcement Learning
  • 【Linux小命令】一文讲清ldd命令及使用场景
  • 自费5K,测评安德迈、小米、希喂三款宠物空气净化器谁才是高性价比之王
  • 1373. 二叉搜索子树的最大键值和
  • 基于java + Springboot 的二手物品交易平台实现
  • Shopee本土店选品有什么技巧?EasyBoss ERP为你整理了6个高效选品的方法!
  • 3D在线展览馆的独特魅力,技术如何重塑展览业的未来?
  • 基于SpringBoot的藏区特产销售平台
  • hudi系列-schema evolution(一)
  • Redis-实战篇-缓存雪崩
  • 线性代数|机器学习-P18快速下降奇异值
  • 本地离线模型搭建指南-中文大语言模型底座选择依据
  • 【代码随想录】【算法训练营】【第51天】 [115]不同的子序列 [583]两个字符串的删除操作 [72]编辑距离