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

Merge pull request !507 from 小怪兽说疼疼哒/v5-dev
This commit is contained in:
Looly 2022-01-19 01:10:54 +00:00 committed by Gitee
commit ee80e8c195
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 15 additions and 1 deletions

View File

@ -164,6 +164,11 @@ public class NumberChineseFormatter {
*/
public static String formatThousand(int amount, boolean isUseTraditional){
Assert.checkBetween(amount, -999, 999, "Number support only: (-999 ~ 999)");
// thousandToChinese方法对0不处理此处直接返回""
if (amount == 0) {
return String.valueOf(DIGITS[0]);
}
final String chinese = thousandToChinese(amount, isUseTraditional);
if(amount < 20 && amount > 10){
// "十一"而非"一十一"

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