|
@@ -1,237 +0,0 @@
|
|
|
-package vip.xiaonuo.coldchain.core.util;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author jackzhou
|
|
|
- * @version 1.0
|
|
|
- * @project jfcloud-coldchain
|
|
|
- * @description
|
|
|
- * @date 2024/11/22 14:50:28
|
|
|
- */
|
|
|
-
|
|
|
-import vip.xiaonuo.coldchain.modular.app.param.AggregationWindow;
|
|
|
-
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.time.Instant;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneId;
|
|
|
-import java.time.ZonedDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.time.temporal.ChronoUnit;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Locale;
|
|
|
-
|
|
|
-public class DateFormatter {
|
|
|
-
|
|
|
- // 批量格式化多个 Date 对象的方法
|
|
|
- public List<String> getFormattedLastUpdated(List<Date> dateList) {
|
|
|
- List<String> formattedDates = new ArrayList<>();
|
|
|
- for (Date lastUpdated : dateList) {
|
|
|
- // 如果 lastUpdated 为 null,返回默认值
|
|
|
- if (lastUpdated == null) {
|
|
|
- formattedDates.add(""); // 可以返回适当的默认值
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 将 Date 转换为 LocalDateTime
|
|
|
- LocalDateTime lastUpdatedLocal = lastUpdated.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
-
|
|
|
- // 获取当前时间
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
-
|
|
|
- // 计算两个日期的差值(以天为单位)
|
|
|
- long daysDiff = ChronoUnit.DAYS.between(lastUpdatedLocal, now);
|
|
|
-
|
|
|
- // 根据日期差异来格式化输出
|
|
|
- if (daysDiff <= 7) {
|
|
|
- // 使用 Locale.CHINESE 进行格式化,显示中文星期几
|
|
|
- DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.SIMPLIFIED_CHINESE);
|
|
|
- formattedDates.add(lastUpdatedLocal.format(dayFormatter)); // 返回中文星期几
|
|
|
- } else {
|
|
|
- // 超过 7 天,显示完整的日期时间格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- formattedDates.add(lastUpdatedLocal.format(formatter)); // 返回完整的日期时间
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return formattedDates;
|
|
|
- }
|
|
|
-
|
|
|
- // 单个 Date 格式化的方法
|
|
|
- public String getFormattedSingleDate(Date lastUpdated) {
|
|
|
- // 如果 lastUpdated 为 null,返回默认值
|
|
|
- if (lastUpdated == null) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- // 将 Date 转换为 LocalDateTime
|
|
|
- LocalDateTime lastUpdatedLocal = lastUpdated.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
- // 获取当前时间
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
- // 计算两个日期的差值(以天为单位)
|
|
|
- long daysDiff = ChronoUnit.DAYS.between(lastUpdatedLocal, now);
|
|
|
- // 根据日期差异来格式化输出
|
|
|
- if (daysDiff <= 7) {
|
|
|
- // 使用 Locale.CHINESE 进行格式化,显示中文星期几
|
|
|
- DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.SIMPLIFIED_CHINESE);
|
|
|
- return lastUpdatedLocal.format(dayFormatter); // 返回中文星期几
|
|
|
- } else {
|
|
|
- // 超过 7 天,显示完整的日期时间格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- return lastUpdatedLocal.format(formatter); // 返回完整的日期时间
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String getFormattedSingleDate(Instant lastUpdated) {
|
|
|
- // 如果 lastUpdated 为 null,返回默认值
|
|
|
- if (lastUpdated == null) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- // 将 Instant 转换为 LocalDateTime
|
|
|
- LocalDateTime lastUpdatedLocal = LocalDateTime.ofInstant(lastUpdated, ZoneId.systemDefault());
|
|
|
- // 获取当前时间
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
- // 计算两个日期的差值(以天为单位)
|
|
|
- long daysDiff = ChronoUnit.DAYS.between(lastUpdatedLocal, now);
|
|
|
- // 根据日期差异来格式化输出
|
|
|
- if (daysDiff <= 7) {
|
|
|
- // 使用 Locale.CHINESE 进行格式化,显示中文星期几
|
|
|
- DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.SIMPLIFIED_CHINESE);
|
|
|
- return lastUpdatedLocal.format(dayFormatter); // 返回中文星期几
|
|
|
- } else {
|
|
|
- // 超过 7 天,显示完整的日期时间格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- return lastUpdatedLocal.format(formatter); // 返回完整的日期时间
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 格式化 Date 类型
|
|
|
- public static String formatTime(Date date, AggregationWindow aggregationWindow) {
|
|
|
- if (date == null || aggregationWindow == null) {
|
|
|
- throw new IllegalArgumentException("日期时间和聚合窗口不能为空");
|
|
|
- }
|
|
|
- Instant instant = date.toInstant();
|
|
|
- LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
|
|
- return formatTime(localDateTime, aggregationWindow);
|
|
|
- }
|
|
|
-
|
|
|
- // 格式化 String 类型,增加了 dateFormat 参数
|
|
|
- public static String formatTime(String dateTimeStr, AggregationWindow aggregationWindow, String dateFormat) {
|
|
|
- if (dateTimeStr == null || aggregationWindow == null) {
|
|
|
- throw new IllegalArgumentException("日期时间和聚合窗口不能为空");
|
|
|
- }
|
|
|
- if (dateFormat == null) {
|
|
|
- dateFormat = DateTimeFormatter.ISO_LOCAL_DATE_TIME.toString(); // 默认使用 ISO 格式
|
|
|
- }
|
|
|
-
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
|
|
|
- LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, formatter);
|
|
|
- return formatTime(localDateTime, aggregationWindow);
|
|
|
- }
|
|
|
-
|
|
|
- // 格式化 Instant 类型
|
|
|
- public static String formatTime(Instant instant, AggregationWindow aggregationWindow) {
|
|
|
- if (instant == null || aggregationWindow == null) {
|
|
|
- throw new IllegalArgumentException("日期时间和聚合窗口不能为空");
|
|
|
- }
|
|
|
- LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
|
|
- return formatTime(localDateTime, aggregationWindow);
|
|
|
- }
|
|
|
-
|
|
|
- // 格式化 LocalDateTime 类型
|
|
|
- public static String formatTime(LocalDateTime localDateTime, AggregationWindow aggregationWindow) {
|
|
|
- if (localDateTime == null || aggregationWindow == null) {
|
|
|
- throw new IllegalArgumentException("日期时间和聚合窗口不能为空");
|
|
|
- }
|
|
|
-
|
|
|
- // 定义日期时间格式
|
|
|
- String pattern;
|
|
|
- switch (aggregationWindow) {
|
|
|
- case MINUTE:
|
|
|
- pattern = "yyyy-MM-dd HH:mm"; // 精确到分钟
|
|
|
- break;
|
|
|
- case HOUR:
|
|
|
- pattern = "yyyy-MM-dd HH"; // 精确到小时
|
|
|
- break;
|
|
|
- case HALF_DAY:
|
|
|
- pattern = "yyyy-MM-dd a"; // 精确到上午/下午
|
|
|
- break;
|
|
|
- case DAY:
|
|
|
- pattern = "yyyy-MM-dd"; // 精确到天
|
|
|
- break;
|
|
|
- case WEEK:
|
|
|
- pattern = "yyyy年 第w周"; // 中文自然周
|
|
|
- break;
|
|
|
- case MONTH:
|
|
|
- pattern = "yyyy年MM月"; // 精确到月
|
|
|
- break;
|
|
|
- case QUARTER:
|
|
|
- // 计算季度:将月份(1-12)映射为中文的"第一季度"至"第四季度"
|
|
|
- int month = localDateTime.getMonthValue();
|
|
|
- String quarter = "第" + ((month - 1) / 3 + 1) + "季度";
|
|
|
- return localDateTime.getYear() + "年" + quarter; // 格式如 "2024年第一季度"
|
|
|
- case YEAR:
|
|
|
- pattern = "yyyy年"; // 精确到年
|
|
|
- break;
|
|
|
- default:
|
|
|
- pattern = "yyyy-MM-dd"; // 默认精确到天
|
|
|
- break;
|
|
|
- }
|
|
|
- // 返回格式化的日期时间
|
|
|
- return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
|
|
|
- }
|
|
|
-
|
|
|
- public static String convertToUTCPlus8(String time) {
|
|
|
- // 1. 解析时间为 Instant (UTC)
|
|
|
- Instant instant = Instant.parse(time); // time 需要是 ISO 8601 格式,例如 "2024-11-25T07:42:48.861094300Z"
|
|
|
- // 2. 将 Instant 转换为 UTC+8 (Asia/Shanghai)
|
|
|
- ZonedDateTime utcPlus8Time = instant.atZone(ZoneId.of("Asia/Shanghai"));
|
|
|
- // 3. 格式化为所需的字符串输出
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
- return formatter.format(utcPlus8Time);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String instantToUTC8String(Instant instant) {
|
|
|
- // 1. 将 Instant 转换为指定时区的 ZonedDateTime
|
|
|
- ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Shanghai"));
|
|
|
- // 2. 格式化为目标格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- // 3. 返回格式化后的时间字符串
|
|
|
- return formatter.format(zonedDateTime);
|
|
|
- }
|
|
|
-
|
|
|
- public static String now(Date instant) {
|
|
|
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- // 3. 返回格式化后的时间字符串
|
|
|
- return formatter.format(instant);
|
|
|
- }
|
|
|
-
|
|
|
- public static String replaceDateFormat(String input) {
|
|
|
- if (input == null || input.isEmpty()) {
|
|
|
- return input;
|
|
|
- }
|
|
|
- // 将"YYYY/MM/DD"替换为"YYYY-MM-DD"
|
|
|
- return input.trim().replaceAll("(\\d{4})/(\\d{2})/(\\d{2})", "$1-$2-$3");
|
|
|
- }
|
|
|
-
|
|
|
-// public static void main(String[] args) {
|
|
|
-// // 示例:传入 Date 类型
|
|
|
-// Date now = new Date();
|
|
|
-// System.out.println(DateFormatter.formatTime(now, AggregationWindow.DAY)); // 2024-12-10
|
|
|
-//
|
|
|
-// // 示例:传入 String 类型并指定日期格式
|
|
|
-// String dateStr = "12-10-2024 10:30";
|
|
|
-// String dateFormat = "MM-dd-yyyy HH:mm"; // 指定自定义的日期格式
|
|
|
-// System.out.println(DateFormatter.formatTime(dateStr, AggregationWindow.HOUR, dateFormat)); // 2024-12-10 10
|
|
|
-//
|
|
|
-// // 示例:传入 Instant 类型
|
|
|
-// Instant instantNow = Instant.now();
|
|
|
-// System.out.println(DateFormatter.formatTime(instantNow, AggregationWindow.QUARTER)); // 2024年第四季度
|
|
|
-//
|
|
|
-// // 示例:传入 LocalDateTime 类型
|
|
|
-// LocalDateTime localDateTimeNow = LocalDateTime.now();
|
|
|
-// System.out.println(DateFormatter.formatTime(localDateTimeNow, AggregationWindow.MONTH)); // 2024年12月
|
|
|
-// }
|
|
|
-
|
|
|
-}
|
|
|
-
|