mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
[improve] 完善注释和优化单元测试
This commit is contained in:
parent
30ea0060a5
commit
b9cf5e48c7
@ -168,6 +168,14 @@ public class DateBetween implements Serializable {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getBegin() {
|
||||||
|
return begin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getEnd() {
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化输出时间差
|
* 格式化输出时间差
|
||||||
*
|
*
|
||||||
|
@ -9,8 +9,22 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ChangAnTimeConverter
|
* 长安时辰转换器
|
||||||
*
|
* <p>
|
||||||
|
* 23-1 -> 子时
|
||||||
|
* 1-3 -> 丑时
|
||||||
|
* 3-5 -> 寅时
|
||||||
|
* 5-7 -> 卯时
|
||||||
|
* 7-9 -> 辰时
|
||||||
|
* 9-11 -> 巳时
|
||||||
|
* 11-13 -> 午时
|
||||||
|
* 13-15 -> 未时
|
||||||
|
* 15-17 -> 申时
|
||||||
|
* 17-19 -> 酉时
|
||||||
|
* 19-21 -> 戌时
|
||||||
|
* 21-23 -> 亥时
|
||||||
|
* 24/-1/其他值 -> 未知
|
||||||
|
* </p>
|
||||||
* @author achao@hutool.cn
|
* @author achao@hutool.cn
|
||||||
*/
|
*/
|
||||||
public class ChangAnTimeConverter {
|
public class ChangAnTimeConverter {
|
||||||
@ -33,6 +47,16 @@ public class ChangAnTimeConverter {
|
|||||||
timeMap.put("亥", new int[]{21, 23});
|
timeMap.put("亥", new int[]{21, 23});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将长安时辰转换为现代时间
|
||||||
|
* <p>
|
||||||
|
* toModernTime("子时").getBegin().getHours() -> 23
|
||||||
|
* toModernTime("子时").getEnd().getHours() -> 1
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param changAnTime 长安时辰
|
||||||
|
* @return 现代时间段
|
||||||
|
*/
|
||||||
public static DateBetween toModernTime(String changAnTime) {
|
public static DateBetween toModernTime(String changAnTime) {
|
||||||
String time = changAnTime.replace("时", "");
|
String time = changAnTime.replace("时", "");
|
||||||
int[] hours = timeMap.get(time);
|
int[] hours = timeMap.get(time);
|
||||||
@ -53,6 +77,14 @@ public class ChangAnTimeConverter {
|
|||||||
return DateBetween.of(startDate, endDate);
|
return DateBetween.of(startDate, endDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将小时转换为长安时辰
|
||||||
|
* <p>
|
||||||
|
* toChangAnTime(1) -> "子时"
|
||||||
|
*</p>
|
||||||
|
* @param hour 小时
|
||||||
|
* @return 长安时辰
|
||||||
|
*/
|
||||||
public static String toChangAnTime(int hour) {
|
public static String toChangAnTime(int hour) {
|
||||||
for (Map.Entry<String, int[]> entry : timeMap.entrySet()) {
|
for (Map.Entry<String, int[]> entry : timeMap.entrySet()) {
|
||||||
int startHour = entry.getValue()[0];
|
int startHour = entry.getValue()[0];
|
||||||
@ -61,7 +93,7 @@ public class ChangAnTimeConverter {
|
|||||||
return entry.getKey() + "时";
|
return entry.getKey() + "时";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "未知时";
|
return "未知";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,23 @@ public class ChangAnTimeConverterTest {
|
|||||||
void testToModernTimeForAllTimes() {
|
void testToModernTimeForAllTimes() {
|
||||||
// 测试每个时辰的转换
|
// 测试每个时辰的转换
|
||||||
String[] times = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
|
String[] times = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
|
||||||
int[] expectedHours = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
|
int[][] expectedHours = {{23, 1}, {1, 3}, {3, 5}, {5, 7}, {7, 9}, {9, 11}, {11, 13}, {13, 15}, {15, 17}, {17, 19}, {19, 21}, {21, 23}};
|
||||||
for (int i = 0; i < times.length; i++) {
|
for (int i = 0; i < times.length; i++) {
|
||||||
DateBetween dateBetween = ChangAnTimeConverter.toModernTime(times[i] + "时");
|
DateBetween dateBetween = ChangAnTimeConverter.toModernTime(times[i] + "时");
|
||||||
long hoursBetween = dateBetween.between(DateUnit.HOUR);
|
Assertions.assertEquals(2, dateBetween.between(DateUnit.HOUR));
|
||||||
Assertions.assertEquals(expectedHours[i], hoursBetween, times[i] + "时 should last for 2 hours.");
|
Assertions.assertEquals(expectedHours[i][0], dateBetween.getBegin().getHours());
|
||||||
|
Assertions.assertEquals(expectedHours[i][1], dateBetween.getEnd().getHours());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToChangAnTimeForAllHours() {
|
void testToChangAnTimeForAllHours() {
|
||||||
// 从23时开始测试,因为子时开始于23时
|
// 从23时开始测试,因为子时开始于23时
|
||||||
String[] expectedTimes = {"子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时", "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时", "子时"};
|
String[] expectedTimes = {"子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时", "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时", "子时", "未知"};
|
||||||
for (int hour = 0; hour < 24; hour++) {
|
for (int hour = 0; hour <= 24; hour++) {
|
||||||
String expectedTime = expectedTimes[hour];
|
String expectedTime = expectedTimes[hour];
|
||||||
String actualTime = ChangAnTimeConverter.toChangAnTime(hour);
|
String actualTime = ChangAnTimeConverter.toChangAnTime(hour);
|
||||||
Assertions.assertEquals(expectedTime, actualTime, "Hour " + hour + " should be in " + expectedTime);
|
Assertions.assertEquals(expectedTime, actualTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user