DateUtil增加formatChineseDateTime 转化为中文日期格式到时分秒

This commit is contained in:
王健 2020-06-19 20:19:43 +08:00
parent 9d02dcd5d8
commit f93017e49f
3 changed files with 47 additions and 0 deletions

View File

@ -62,6 +62,11 @@ public class DatePattern {
/** 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日 */
public static final FastDateFormat CHINESE_DATE_FORMAT = FastDateFormat.getInstance(CHINESE_DATE_PATTERN);
/** 标准日期格式yyyy年MM月dd日 HH时mm分ss秒 */
public static final String CHINESE_DATE_TIME_PATTERN = "yyyy年MM月dd日HH时mm分ss秒";
/** 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日HH时mm分ss秒*/
public static final FastDateFormat CHINESE_DATE_TIME_FORMAT = FastDateFormat.getInstance(CHINESE_DATE_TIME_PATTERN);
//-------------------------------------------------------------------------------------------------------------------------------- Pure
/** 标准日期格式yyyyMMdd */
public static final String PURE_DATE_PATTERN = "yyyyMMdd";

View File

@ -636,6 +636,42 @@ public class DateUtil extends CalendarUtil {
}
return format;
}
/**
* 格式化为中文日期格式如果isUppercase为false则返回类似2018年10月24日 12时13分14秒否则返回二一八年十月二十四日 十二时十三分十四秒
*
* @param date 被格式化的日期
* @param isUppercase 是否采用大写形式
* @return 中文日期字符串
* @since 4.1.19
*/
public static String formatChineseDateTime(Date date, boolean isUppercase) {
if (null == date) {
return null;
}
String format = DatePattern.CHINESE_DATE_TIME_FORMAT.format(date);
if (isUppercase) {
final StringBuilder builder = StrUtil.builder(format.length());
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(0, 1)), false));
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(1, 2)), false));
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(2, 3)), false));
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(3, 4)), false));
builder.append(format, 4, 5);
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(5, 7)), false));
builder.append(format, 7, 8);
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(8, 10)), false));
builder.append(format, 10, 11);
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(11, 13)), false));
builder.append(format, 13, 14);
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(14, 16)), false));
builder.append(format, 16, 17);
builder.append(Convert.numberToChinese(Integer.parseInt(format.substring(17, 19)), false));
builder.append(format.substring(19));
format = builder.toString().replace('零', '');
}
return format;
}
// ------------------------------------ Format end ----------------------------------------------
// ------------------------------------ Parse start ----------------------------------------------

View File

@ -224,6 +224,12 @@ public class DateUtilTest {
Assert.assertEquals("二〇一八年二月二十四日", formatChineseDate);
}
@Test
public void formatChineseDateTimeTest() {
String formatChineseDateTime = DateUtil.formatChineseDateTime(DateUtil.parse("2018-02-24 12:13:14"), true);
Assert.assertEquals("二〇一八年二月二十四日一十二时一十三分一十四秒", formatChineseDateTime);
}
@Test
public void formatBetweenTest() {
String dateStr1 = "2017-03-01 22:34:23";