fix date chinese bug

This commit is contained in:
looly 2021-12-01 16:44:05 +08:00
parent 83108c712d
commit a7fde0338b
4 changed files with 89 additions and 39 deletions

View File

@ -40,6 +40,7 @@
* 【poi 】 修复读取日期类型的自定义样式单元格时间结果为1899年问题pr#1977@Github
* 【poi 】 修复SoapClient参数未使用问题
* 【core 】 修复HashUtil.cityHash128参数未使用问题
* 【core 】 修复DateUtil.formatChineseDate显示问题issue#I4KK5F@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -64,7 +64,7 @@ public class NumberChineseFormatter {
return "";
}
Assert.checkBetween(amount, -99_9999_9999_9999.99, 99_9999_9999_9999.99,
"Number support only: (-99999999999999.99 99999999999999.99)");
"Number support only: (-99999999999999.99 ~ 99999999999999.99)");
final StringBuilder chineseStr = new StringBuilder();
@ -126,6 +126,52 @@ public class NumberChineseFormatter {
return chineseStr.toString();
}
/**
* 阿拉伯数字支持正负整数转换成中文
*
* @param amount 数字
* @param isUseTraditional 是否使用繁体
* @return 中文
* @since 5.7.17
*/
public static String format(long amount, boolean isUseTraditional){
if(0 == amount){
return "";
}
Assert.checkBetween(amount, -99_9999_9999_9999.99, 99_9999_9999_9999.99,
"Number support only: (-99999999999999.99 ~ 99999999999999.99)");
final StringBuilder chineseStr = new StringBuilder();
// 负数
if (amount < 0) {
chineseStr.append("");
amount = -amount;
}
chineseStr.append(longToChinese(amount, isUseTraditional));
return chineseStr.toString();
}
/**
* 格式化-999~999之间的数字<br>
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"
*
* @param amount 数字
* @param isUseTraditional 是否使用繁体
* @return 中文
* @since 5.7.17
*/
public static String formatThousand(int amount, boolean isUseTraditional){
Assert.checkBetween(amount, -999, 999, "Number support only: (-999 ~ 999)");
final String chinese = thousandToChinese(amount, isUseTraditional);
if(amount < 20 && amount > 10){
// "十一"而非"一十一"
return chinese.substring(1);
}
return chinese;
}
/**
* 数字字符转中文非数字字符原样返回
*

View File

@ -526,7 +526,7 @@ public class CalendarUtil {
*
* <pre>
* 2018-02-24 12:13:14 转换为 一八年二月二十四日withTime为false
* 2018-02-24 12:13:14 转换为 一八年二月二十四日十二时十三分十四秒withTime为true
* 2018-02-24 12:13:14 转换为 一八年二月二十四日十二时十三分十四秒withTime为true
* </pre>
*
* @param calendar {@link Calendar}
@ -538,7 +538,7 @@ public class CalendarUtil {
final StringBuilder result = StrUtil.builder();
//
String year = String.valueOf(calendar.get(Calendar.YEAR));
final String year = String.valueOf(calendar.get(Calendar.YEAR));
final int length = year.length();
for (int i = 0; i < length; i++) {
result.append(NumberChineseFormatter.numberCharToChinese(year.charAt(i), false));
@ -547,26 +547,26 @@ public class CalendarUtil {
//
int month = calendar.get(Calendar.MONTH) + 1;
result.append(NumberChineseFormatter.format(month, false));
result.append(NumberChineseFormatter.formatThousand(month, false));
result.append('月');
//
int day = calendar.get(Calendar.DAY_OF_MONTH);
result.append(NumberChineseFormatter.format(day, false));
result.append(NumberChineseFormatter.formatThousand(day, false));
result.append('日');
if (withTime) {
//
int hour = calendar.get(Calendar.HOUR_OF_DAY);
result.append(NumberChineseFormatter.format(hour, false));
result.append(NumberChineseFormatter.formatThousand(hour, false));
result.append('时');
//
int minute = calendar.get(Calendar.MINUTE);
result.append(NumberChineseFormatter.format(minute, false));
result.append(NumberChineseFormatter.formatThousand(minute, false));
result.append('分');
//
int second = calendar.get(Calendar.SECOND);
result.append(NumberChineseFormatter.format(second, false));
result.append(NumberChineseFormatter.formatThousand(second, false));
result.append('秒');
}

View File

@ -279,12 +279,15 @@ public class DateUtilTest {
public void formatChineseDateTest() {
String formatChineseDate = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24"), true, false);
Assert.assertEquals("二〇一八年二月二十四日", formatChineseDate);
formatChineseDate = DateUtil.formatChineseDate(DateUtil.parse("2018-02-14"), true, false);
Assert.assertEquals("二〇一八年二月十四日", formatChineseDate);
}
@Test
public void formatChineseDateTimeTest() {
String formatChineseDateTime = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24 12:13:14"), true, true);
Assert.assertEquals("二〇一八年二月二十四日十二时十三分十四秒", formatChineseDateTime);
Assert.assertEquals("二〇一八年二月二十四日十二时十三分十四秒", formatChineseDateTime);
}
@Test