mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix date chinese bug
This commit is contained in:
parent
83108c712d
commit
a7fde0338b
@ -40,6 +40,7 @@
|
||||
* 【poi 】 修复读取日期类型的自定义样式单元格时间结果为1899年问题(pr#1977@Github)
|
||||
* 【poi 】 修复SoapClient参数未使用问题
|
||||
* 【core 】 修复HashUtil.cityHash128参数未使用问题
|
||||
* 【core 】 修复DateUtil.formatChineseDate显示问题(issue#I4KK5F@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数字字符转中文,非数字字符原样返回
|
||||
*
|
||||
|
@ -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('秒');
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user