From 30ea0060a57433fc2ecbf133f7015fa3a02dbf20 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Sun, 25 Feb 2024 20:59:14 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20=E6=B7=BB=E5=8A=A0=E9=95=BF?= =?UTF-8?q?=E5=AE=89=E5=8D=81=E4=BA=8C=E6=97=B6=E8=BE=B0=E4=B8=8E=E7=8E=B0?= =?UTF-8?q?=E4=BB=A3=E6=97=B6=E9=97=B4=E7=9A=84=E4=BA=92=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../date/chinese/ChangAnTimeConverter.java | 67 +++++++++++++++++++ .../chinese/ChangAnTimeConverterTest.java | 38 +++++++++++ 2 files changed, 105 insertions(+) create mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverter.java create mode 100644 hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverterTest.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverter.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverter.java new file mode 100644 index 000000000..7505c8263 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverter.java @@ -0,0 +1,67 @@ +package org.dromara.hutool.core.date.chinese; + +import org.dromara.hutool.core.date.DateBetween; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * ChangAnTimeConverter + * + * @author achao@hutool.cn + */ +public class ChangAnTimeConverter { + + private static final Map timeMap = new HashMap<>(); + + static { + // 初始化时辰对应的小时范围 + timeMap.put("子", new int[]{23, 1}); + timeMap.put("丑", new int[]{1, 3}); + timeMap.put("寅", new int[]{3, 5}); + timeMap.put("卯", new int[]{5, 7}); + timeMap.put("辰", new int[]{7, 9}); + timeMap.put("巳", new int[]{9, 11}); + timeMap.put("午", new int[]{11, 13}); + timeMap.put("未", new int[]{13, 15}); + timeMap.put("申", new int[]{15, 17}); + timeMap.put("酉", new int[]{17, 19}); + timeMap.put("戌", new int[]{19, 21}); + timeMap.put("亥", new int[]{21, 23}); + } + + public static DateBetween toModernTime(String changAnTime) { + String time = changAnTime.replace("时", ""); + int[] hours = timeMap.get(time); + if (hours == null) { + throw new IllegalArgumentException("Invalid ChangAn time"); + } + + LocalDateTime now = LocalDateTime.now(); + LocalDateTime start = now.withHour(hours[0]).withMinute(0).withSecond(0).withNano(0); + LocalDateTime end = now.withHour(hours[1]).withMinute(0).withSecond(0).withNano(0); + if (hours[0] >= hours[1]) { + end = end.plusDays(1); // 处理跨日情况 + } + + Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); + Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); + + return DateBetween.of(startDate, endDate); + } + + public static String toChangAnTime(int hour) { + for (Map.Entry entry : timeMap.entrySet()) { + int startHour = entry.getValue()[0]; + int endHour = entry.getValue()[1]; + if (hour == 23 || hour == 0 || (hour >= startHour && hour < endHour) || (startHour > endHour && hour < endHour)) { + return entry.getKey() + "时"; + } + } + return "未知时"; + } + +} diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverterTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverterTest.java new file mode 100644 index 000000000..d14d81099 --- /dev/null +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/ChangAnTimeConverterTest.java @@ -0,0 +1,38 @@ +package org.dromara.hutool.core.date.chinese; + +import org.dromara.hutool.core.date.DateBetween; +import org.dromara.hutool.core.date.DateUnit; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * ChangAnTimeConverterTest + * + * @author achao@apache.org + */ +public class ChangAnTimeConverterTest { + + @Test + void testToModernTimeForAllTimes() { + // 测试每个时辰的转换 + String[] times = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; + int[] expectedHours = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; + for (int i = 0; i < times.length; i++) { + DateBetween dateBetween = ChangAnTimeConverter.toModernTime(times[i] + "时"); + long hoursBetween = dateBetween.between(DateUnit.HOUR); + Assertions.assertEquals(expectedHours[i], hoursBetween, times[i] + "时 should last for 2 hours."); + } + } + + @Test + void testToChangAnTimeForAllHours() { + // 从23时开始测试,因为子时开始于23时 + String[] expectedTimes = {"子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时", "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时", "子时"}; + for (int hour = 0; hour < 24; hour++) { + String expectedTime = expectedTimes[hour]; + String actualTime = ChangAnTimeConverter.toChangAnTime(hour); + Assertions.assertEquals(expectedTime, actualTime, "Hour " + hour + " should be in " + expectedTime); + } + } + +}