mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
DateUtil增加formatChineseDateTime 转化为中文日期格式到时分秒
This commit is contained in:
parent
9d02dcd5d8
commit
f93017e49f
@ -62,6 +62,11 @@ public class DatePattern {
|
|||||||
/** 标准日期格式 {@link FastDateFormat}:yyyy年MM月dd日 */
|
/** 标准日期格式 {@link FastDateFormat}:yyyy年MM月dd日 */
|
||||||
public static final FastDateFormat CHINESE_DATE_FORMAT = FastDateFormat.getInstance(CHINESE_DATE_PATTERN);
|
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
|
//-------------------------------------------------------------------------------------------------------------------------------- Pure
|
||||||
/** 标准日期格式:yyyyMMdd */
|
/** 标准日期格式:yyyyMMdd */
|
||||||
public static final String PURE_DATE_PATTERN = "yyyyMMdd";
|
public static final String PURE_DATE_PATTERN = "yyyyMMdd";
|
||||||
|
@ -636,6 +636,42 @@ public class DateUtil extends CalendarUtil {
|
|||||||
}
|
}
|
||||||
return format;
|
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 ----------------------------------------------
|
// ------------------------------------ Format end ----------------------------------------------
|
||||||
|
|
||||||
// ------------------------------------ Parse start ----------------------------------------------
|
// ------------------------------------ Parse start ----------------------------------------------
|
||||||
|
@ -223,6 +223,12 @@ public class DateUtilTest {
|
|||||||
String formatChineseDate = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24"), true);
|
String formatChineseDate = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24"), true);
|
||||||
Assert.assertEquals("二〇一八年二月二十四日", formatChineseDate);
|
Assert.assertEquals("二〇一八年二月二十四日", formatChineseDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatChineseDateTimeTest() {
|
||||||
|
String formatChineseDateTime = DateUtil.formatChineseDateTime(DateUtil.parse("2018-02-24 12:13:14"), true);
|
||||||
|
Assert.assertEquals("二〇一八年二月二十四日一十二时一十三分一十四秒", formatChineseDateTime);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatBetweenTest() {
|
public void formatBetweenTest() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user