修复:格式化为中文日期时,0被处理为空串

This commit is contained in:
tjh 2022-01-18 16:12:03 +08:00
parent 4b02bc7287
commit 8c20364c2c
2 changed files with 10 additions and 1 deletions

View File

@ -555,6 +555,12 @@ public class CalendarUtil {
result.append(NumberChineseFormatter.formatThousand(day, false));
result.append('日');
// 时分秒中零不需要替换
String temp = result.toString().replace('零', '');
result.delete(0, result.length());
result.append(temp);
if (withTime) {
//
int hour = calendar.get(Calendar.HOUR_OF_DAY);
@ -570,7 +576,7 @@ public class CalendarUtil {
result.append('秒');
}
return result.toString().replace('零', '');
return result.toString();
}
/**

View File

@ -289,6 +289,9 @@ public class DateUtilTest {
public void formatChineseDateTimeTest() {
String formatChineseDateTime = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24 12:13:14"), true, true);
Assert.assertEquals("二〇一八年二月二十四日十二时十三分十四秒", formatChineseDateTime);
formatChineseDateTime = DateUtil.formatChineseDate(DateUtil.parse("2022-01-18 12:00:00"), true, true);
Assert.assertEquals("二〇二二年一月十八日十二时零分零秒", formatChineseDateTime);
}
@Test