This commit is contained in:
Looly 2024-02-27 09:15:45 +08:00
parent 648ffb20a6
commit dbc1a24b07
2 changed files with 37 additions and 35 deletions

View File

@ -168,10 +168,20 @@ public class DateBetween implements Serializable {
return result; return result;
} }
/**
* 获取开始时间
*
* @return 获取开始时间
*/
public Date getBegin() { public Date getBegin() {
return begin; return begin;
} }
/**
* 获取结束日期
*
* @return 结束日期
*/
public Date getEnd() { public Date getEnd() {
return end; return end;
} }

View File

@ -13,19 +13,16 @@ import java.util.Map;
* 时辰转换器支持宋以后的二十四时辰制度 * 时辰转换器支持宋以后的二十四时辰制度
* <p>本转换器提供以下功能 * <p>本转换器提供以下功能
* <ul> * <ul>
* <li>处理包含后缀的长安时辰描述并自动返回相应的现代时间段 * <li>处理包含后缀的时辰描述并自动返回相应的现代时间段
* 分别对应每个时辰的前半段和后半段而不带后缀的描述则涵盖该时辰的完整时间段</li> * 分别对应每个时辰的前半段和后半段而不带后缀的描述则涵盖该时辰的完整时间段</li>
* <li>根据小时数转换为相应的长安时辰描述通过{@code isAbs}参数控制是否包含</li> * <li>根据小时数转换为相应的时辰描述通过{@code isAbs}参数控制是否包含</li>
* </ul> * </ul>
* </p>
* <p> * <p>
* 异常情况 * 异常情况
* <ul> * <ul>
* <li>如果输入的长安时辰描述无效或不被识别{@code toModernTime} 方法将抛出 {@code IllegalArgumentException}</li> * <li>如果输入的时辰描述无效或不被识别{@code toModernTime} 方法将抛出 {@code IllegalArgumentException}</li>
* <li>同样如果{@code toShiChen}方法接收到无效的小时数将返回未知</li> * <li>同样如果{@code toShiChen}方法接收到无效的小时数将返回未知</li>
* </ul> * </ul>
* </p>
* <p>
* 示例 * 示例
* <ul> * <ul>
* <li>{@code toModernTime("子时")} 返回的时间段从23点开始到1点结束</li> * <li>{@code toModernTime("子时")} 返回的时间段从23点开始到1点结束</li>
@ -34,7 +31,6 @@ import java.util.Map;
* <li>{@code toShiChen(0, false)} 返回子正</li> * <li>{@code toShiChen(0, false)} 返回子正</li>
* <li>{@code toShiChen(0, true)} 返回子时</li> * <li>{@code toShiChen(0, true)} 返回子时</li>
* </ul> * </ul>
* </p>
* *
* @author achao@hutool.cn * @author achao@hutool.cn
*/ */
@ -47,9 +43,9 @@ public class ShiChen {
static { static {
// 初始化时辰对应的小时范围 // 初始化时辰对应的小时范围
String[] times = {"", "", "", "", "", "", "", "", "", "", "", ""}; final String[] times = {"", "", "", "", "", "", "", "", "", "", "", ""};
int hour = 23; int hour = 23;
for (String time : times) { for (final String time : times) {
timeMap.put(time + "", hour % 24); timeMap.put(time + "", hour % 24);
timeMap.put(time + "", (hour + 1) % 24); timeMap.put(time + "", (hour + 1) % 24);
fullTimeMap.put(time, new Integer[]{hour % 24, (hour + 2) % 24}); fullTimeMap.put(time, new Integer[]{hour % 24, (hour + 2) % 24});
@ -58,7 +54,7 @@ public class ShiChen {
// 初始化小时到时辰的映射 // 初始化小时到时辰的映射
hour = 23; hour = 23;
for (String time : times) { for (final String time : times) {
hourToShiChenMap.put(hour % 24, time + ""); hourToShiChenMap.put(hour % 24, time + "");
hourToShiChenMap.put((hour + 1) % 24, time + ""); hourToShiChenMap.put((hour + 1) % 24, time + "");
hourToShiChenAbsMap.put(hour % 24, time + ""); hourToShiChenAbsMap.put(hour % 24, time + "");
@ -68,38 +64,37 @@ public class ShiChen {
} }
/** /**
* 将长安时辰描述转换为现代时间段 * 将时辰描述转换为现代时间段示例
* <p>
* 示例
* <ul> * <ul>
* <li>{@code toModernTime("子时")} 返回的时间段从23点开始到1点结束</li> * <li>{@code toModernTime("子时")} 返回的时间段从23点开始到1点结束</li>
* <li>{@code toModernTime("子初")} 返回的时间段从23点开始到0点结束</li> * <li>{@code toModernTime("子初")} 返回的时间段从23点开始到0点结束</li>
* <li>{@code toModernTime("子正")} 返回的时间段从0点开始到1点结束</li> * <li>{@code toModernTime("子正")} 返回的时间段从0点开始到1点结束</li>
* </ul> * </ul>
* </p>
* *
* @param shiChen 长安时辰描述可以是结尾 * @param shiChen 时辰描述可以是结尾
* @return {@link DateBetween} 对象表示起始和结束时间 * @return {@link DateBetween} 对象表示起始和结束时间
* @throws IllegalArgumentException 如果输入的长安时辰描述无效 * @throws IllegalArgumentException 如果输入的时辰描述无效
*/ */
public static DateBetween toModernTime(String shiChen) { public static DateBetween toModernTime(final String shiChen) {
if (StrUtil.isEmpty(shiChen)) { if (StrUtil.isEmpty(shiChen)) {
throw new IllegalArgumentException("Invalid shiChen"); throw new IllegalArgumentException("Invalid shiChen");
} }
Integer startHour, endHour; final Integer startHour;
LocalDateTime start, end; final Integer endHour;
final LocalDateTime start;
final LocalDateTime end;
if (shiChen.endsWith("") || shiChen.endsWith("")) { if (shiChen.endsWith("") || shiChen.endsWith("")) {
startHour = timeMap.get(shiChen); startHour = timeMap.get(shiChen);
if (startHour == null) { if (startHour == null) {
throw new IllegalArgumentException("Invalid ChangAn time"); throw new IllegalArgumentException("Invalid ShiChen time");
} }
endHour = (startHour + 1) % 24; endHour = (startHour + 1) % 24;
} else { } else {
String baseTime = shiChen.replace("", ""); final String baseTime = shiChen.replace("", "");
Integer[] hours = fullTimeMap.get(baseTime); final Integer[] hours = fullTimeMap.get(baseTime);
if (hours == null) { if (hours == null) {
throw new IllegalArgumentException("Invalid ChangAn time"); throw new IllegalArgumentException("Invalid ShiChen time");
} }
startHour = hours[0]; startHour = hours[0];
endHour = hours[1]; endHour = hours[1];
@ -108,27 +103,24 @@ public class ShiChen {
start = LocalDateTime.now().withHour(startHour).withMinute(0).withSecond(0).withNano(0); start = LocalDateTime.now().withHour(startHour).withMinute(0).withSecond(0).withNano(0);
end = (startHour > endHour) ? start.plusDays(1).withHour(endHour) : start.withHour(endHour); end = (startHour > endHour) ? start.plusDays(1).withHour(endHour) : start.withHour(endHour);
Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); final Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); final Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
return DateBetween.of(startDate, endDate); return DateBetween.of(startDate, endDate);
} }
/** /**
* 根据给定的小时数转换为对应的长安时辰描述 * 根据给定的小时数转换为对应的时辰描述示例
* <p>
* 示例
* <ul> * <ul>
* <li>{@code toShiChen(0, false)} 返回子正</li> * <li>{@code toShiChen(0, false)} 返回子正</li>
* <li>{@code toShiChen(0, true)} 返回子时</li> * <li>{@code toShiChen(0, true)} 返回子时</li>
* </ul> * </ul>
* </p>
* *
* @param hour 小时数应在0到23之间 * @param hour 小时数应在0到23之间
* @param isAbs 是否返回绝对时辰描述即包含后缀而不是 * @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, "未知"); String result = hourToShiChenAbsMap.getOrDefault(hour, "未知");
if (!isAbs && !result.equals("未知")) { if (!isAbs && !result.equals("未知")) {
result = hourToShiChenMap.get(hour); result = hourToShiChenMap.get(hour);