|
@@ -8,6 +8,8 @@ package vip.xiaonuo.coldchain.core.util;
|
|
|
* @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;
|
|
@@ -102,51 +104,81 @@ public class DateFormatter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String getFormattedSingleDateHourse(Instant lastUpdated) {
|
|
|
- // 如果 lastUpdated 为 null,返回默认值
|
|
|
- if (lastUpdated == null) {
|
|
|
- return "";
|
|
|
+ // 格式化 Date 类型
|
|
|
+ public static String formatTime(Date date, AggregationWindow aggregationWindow) {
|
|
|
+ if (date == null || aggregationWindow == null) {
|
|
|
+ throw new IllegalArgumentException("日期时间和聚合窗口不能为空");
|
|
|
}
|
|
|
- // 将 Instant 转换为 LocalDateTime
|
|
|
- LocalDateTime lastUpdatedLocal = LocalDateTime.ofInstant(lastUpdated, ZoneId.systemDefault());
|
|
|
-// // 获取当前时间
|
|
|
-// LocalDateTime now = LocalDateTime.now();
|
|
|
-// // 计算两个日期的差值(以天为单位)
|
|
|
-// long daysDiff = ChronoUnit.DAYS.between(lastUpdatedLocal, now);
|
|
|
-// // 始终使用日期和小时分钟秒的格式
|
|
|
-// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd HH:mm");
|
|
|
-// if (daysDiff == 0) {
|
|
|
-// // 如果是今天,返回 "HH:mm" 格式
|
|
|
-// return lastUpdatedLocal.format(DateTimeFormatter.ofPattern("HH:mm"));
|
|
|
-// } else if (daysDiff > 0 && daysDiff <= 7) {
|
|
|
-// // 如果是过去一周以内,使用中文星期几 + 小时
|
|
|
-// DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.SIMPLIFIED_CHINESE);
|
|
|
-// return lastUpdatedLocal.format(dayFormatter) + " " + lastUpdatedLocal.format(DateTimeFormatter.ofPattern("HH:mm"));
|
|
|
-// } else {
|
|
|
-// // 超过 7 天,显示完整的日期时间格式
|
|
|
-// return lastUpdatedLocal.format(formatter);
|
|
|
-// }
|
|
|
- // 始终使用日期和小时分钟秒的格式
|
|
|
- DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("MM-dd");
|
|
|
- return lastUpdatedLocal.format(dayFormatter);
|
|
|
+ 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 格式
|
|
|
+ }
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- // 当前时间
|
|
|
- Instant now = Instant.now();
|
|
|
-
|
|
|
- // 创建一些不同的时间点来进行测试
|
|
|
- Instant recentTime = now.minus(3, ChronoUnit.DAYS); // 3 天前
|
|
|
- Instant olderTime = now.minus(10, ChronoUnit.DAYS); // 10 天前
|
|
|
- Instant exactlyNow = now; // 当前时间
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
|
|
|
+ LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, formatter);
|
|
|
+ return formatTime(localDateTime, aggregationWindow);
|
|
|
+ }
|
|
|
|
|
|
- // 输出测试结果
|
|
|
- System.out.println("最近的时间(3天前): " + getFormattedSingleDateHourse(recentTime));
|
|
|
- System.out.println("较早的时间(10天前): " + getFormattedSingleDateHourse(olderTime));
|
|
|
- System.out.println("当前时间: " + getFormattedSingleDateHourse(exactlyNow));
|
|
|
+ // 格式化 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)
|
|
@@ -181,5 +213,25 @@ public class DateFormatter {
|
|
|
// 将"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月
|
|
|
+// }
|
|
|
+
|
|
|
}
|
|
|
|