From dbc1a24b07f210e05ab770041625ec7ddc70ae41 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 27 Feb 2024 09:15:45 +0800 Subject: [PATCH] fix code --- .../dromara/hutool/core/date/DateBetween.java | 12 +++- .../hutool/core/date/chinese/ShiChen.java | 60 ++++++++----------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/DateBetween.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/DateBetween.java index e78e42253..9369b35ce 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/DateBetween.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/DateBetween.java @@ -152,7 +152,7 @@ public class DateBetween implements Serializable { // 考虑闰年的2月情况 if (Calendar.FEBRUARY == beginCal.get(Calendar.MONTH) && Calendar.FEBRUARY == endCal.get(Calendar.MONTH)) { if (beginCal.get(Calendar.DAY_OF_MONTH) == beginCal.getActualMaximum(Calendar.DAY_OF_MONTH) - && endCal.get(Calendar.DAY_OF_MONTH) == endCal.getActualMaximum(Calendar.DAY_OF_MONTH)) { + && endCal.get(Calendar.DAY_OF_MONTH) == endCal.getActualMaximum(Calendar.DAY_OF_MONTH)) { // 两个日期都位于2月的最后一天,此时月数按照相等对待,此时都设置为1号 beginCal.set(Calendar.DAY_OF_MONTH, 1); endCal.set(Calendar.DAY_OF_MONTH, 1); @@ -168,10 +168,20 @@ public class DateBetween implements Serializable { return result; } + /** + * 获取开始时间 + * + * @return 获取开始时间 + */ public Date getBegin() { return begin; } + /** + * 获取结束日期 + * + * @return 结束日期 + */ public Date getEnd() { return end; } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ShiChen.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ShiChen.java index 799ac571f..9c2f8ceef 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ShiChen.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ShiChen.java @@ -13,19 +13,16 @@ import java.util.Map; * 时辰转换器,支持宋以后的二十四时辰制度。 *

本转换器提供以下功能: *

- *

*

* 异常情况: *

- *

- *

* 示例: *

- *

* * @author achao@hutool.cn */ @@ -47,9 +43,9 @@ public class ShiChen { static { // 初始化时辰对应的小时范围 - String[] times = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; + final String[] times = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; int hour = 23; - for (String time : times) { + for (final String time : times) { timeMap.put(time + "初", hour % 24); timeMap.put(time + "正", (hour + 1) % 24); fullTimeMap.put(time, new Integer[]{hour % 24, (hour + 2) % 24}); @@ -58,7 +54,7 @@ public class ShiChen { // 初始化小时到时辰的映射 hour = 23; - for (String time : times) { + for (final String time : times) { hourToShiChenMap.put(hour % 24, time + "初"); hourToShiChenMap.put((hour + 1) % 24, time + "正"); hourToShiChenAbsMap.put(hour % 24, time + "时"); @@ -68,38 +64,37 @@ public class ShiChen { } /** - * 将长安时辰描述转换为现代时间段。 - *

- * 示例: + * 将时辰描述转换为现代时间段。示例: *

- *

* - * @param shiChen 长安时辰描述,可以是“时”、“初”或“正”结尾。 + * @param shiChen 时辰描述,可以是“时”、“初”或“正”结尾。 * @return {@link DateBetween} 对象,表示起始和结束时间。 - * @throws IllegalArgumentException 如果输入的长安时辰描述无效。 + * @throws IllegalArgumentException 如果输入的时辰描述无效。 */ - public static DateBetween toModernTime(String shiChen) { + public static DateBetween toModernTime(final String shiChen) { if (StrUtil.isEmpty(shiChen)) { throw new IllegalArgumentException("Invalid shiChen"); } - Integer startHour, endHour; - LocalDateTime start, end; + final Integer startHour; + final Integer endHour; + final LocalDateTime start; + final LocalDateTime end; if (shiChen.endsWith("初") || shiChen.endsWith("正")) { startHour = timeMap.get(shiChen); if (startHour == null) { - throw new IllegalArgumentException("Invalid ChangAn time"); + throw new IllegalArgumentException("Invalid ShiChen time"); } endHour = (startHour + 1) % 24; } else { - String baseTime = shiChen.replace("时", ""); - Integer[] hours = fullTimeMap.get(baseTime); + final String baseTime = shiChen.replace("时", ""); + final Integer[] hours = fullTimeMap.get(baseTime); if (hours == null) { - throw new IllegalArgumentException("Invalid ChangAn time"); + throw new IllegalArgumentException("Invalid ShiChen time"); } startHour = hours[0]; endHour = hours[1]; @@ -108,27 +103,24 @@ public class ShiChen { start = LocalDateTime.now().withHour(startHour).withMinute(0).withSecond(0).withNano(0); end = (startHour > endHour) ? start.plusDays(1).withHour(endHour) : start.withHour(endHour); - Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); - Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); + final Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); + final Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); return DateBetween.of(startDate, endDate); } /** - * 根据给定的小时数转换为对应的长安时辰描述。 - *

- * 示例: + * 根据给定的小时数转换为对应的时辰描述。示例: *

- *

* - * @param hour 小时数,应在0到23之间。 + * @param hour 小时数,应在0到23之间。 * @param isAbs 是否返回绝对时辰描述(即包含“时”后缀),而不是“初”或“正”。 - * @return 长安时辰描述,如果小时数无效,则返回“未知”。 + * @return 时辰描述,如果小时数无效,则返回“未知”。 */ - public static String toShiChen(int hour, boolean isAbs) { + public static String toShiChen(final int hour, final boolean isAbs) { String result = hourToShiChenAbsMap.getOrDefault(hour, "未知"); if (!isAbs && !result.equals("未知")) { result = hourToShiChenMap.get(hour);