获取中文日期

在开发过程中,有时会需要获取全中文格式的日期,比如:二〇二四年七月三日
此时就需要将日期转换成该格式,Hutool 封装了该工具:

/**
 * 格式化为中文日期格式,如果isUppercase为false,则返回类似:2018年10月24日,否则返回二〇一八年十月二十四日
 *
 * @param date        被格式化的日期
 * @param isUppercase 是否采用大写形式
 * @param withTime    是否包含时间部分
 * @return 中文日期字符串
 * @since 5.3.9
 */
public static String formatChineseDate(Date date, boolean isUppercase, boolean withTime) {
    if (null == date) {
        return null;
    }

    if (false == isUppercase) {
        return (withTime ? DatePattern.CHINESE_DATE_TIME_FORMAT : DatePattern.CHINESE_DATE_FORMAT).format(date);
    }

    return CalendarUtil.formatChineseDate(CalendarUtil.calendar(date), withTime);
}

进行如下调用即可得到中文日期:

DateUtil.formatChineseDate(DateUtil.parseDate("2024-07-03"), true, false);

输出结果为:二〇二四年七月三日

posted @ 2024-07-03 22:03  天航星  阅读(7)  评论(0编辑  收藏  举报