Java 阿拉伯数字日期转中文日期格式工具类

package com.study.utils;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Description: 中文日期转换工具类
 * @version 1.0
 */
public class CnDateConvertUtil {

    private CnDateConvertUtil(){}

    private static final String [] CN_NUM_STR = new String[]{"〇","一","二","三","四","五","六","七","八","九"};

    private static final String [] CN_DATE_STR = new String[]{"年","月","日","时","分","秒"};

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");

    private static final String CN_TEN_STR = "十";

    private static final String CN_MORNING_STR = "上午";

    private static final String CN_AFTERNOON_STR = "下午";

    /**
     * 阿拉伯数字日期转换成中文格式日期
     * @param localDateTime  输入日期时间,内部转换为字符串日期 yyyy-MM-dd-HH-mm-ss
     * @return  eg:二〇二〇年十二月二十七日上午十一时五十八分
     */
    public  static String dateToCnDate(LocalDateTime localDateTime) {
        if(localDateTime == null){
            return "";
        }
        String formatStr = null;
        try {
            formatStr = FORMATTER.format(localDateTime);
        } catch (Exception e) {
           System.out.println("日期转换出错:"+e.getMessage());
           return "";
        }
        StringBuilder result = new StringBuilder();
        String[] dateStr =formatStr.split("-");
        for (int i=0; i<dateStr.length; i++) {
            String charStr = dateStr[i];
            if(i == 3){
                if(Integer.parseInt(charStr)<12){
                    result.append(CN_MORNING_STR);
                }else{
                    charStr = String.valueOf((Integer.parseInt(charStr)-12));
                    result.append(CN_AFTERNOON_STR);
                }
            }
            for (int j=0; j<charStr.length(); j++) {
                String str = String.valueOf(charStr.charAt(j));
                if (charStr.length() == 2) {
                    if (charStr.equals("10")) {
                        result.append(CN_TEN_STR);
                        break;
                    } else {
                        if (j == 0) {
                            if (charStr.charAt(j) == '1'){
                                result.append(CN_TEN_STR);
                            } else if(charStr.charAt(j) == '0'){
                               // result.append(CN_NUM_STR[0]);
                            } else{
                                result.append(CN_NUM_STR[Integer.parseInt(str)]).append(CN_TEN_STR);
                            }
                        }
                        if (j == 1) {
                            if (charStr.charAt(j) == '0'){
                                result.append(CN_NUM_STR[0]);
                            } else{
                                result.append(CN_NUM_STR[Integer.parseInt(str)]);
                            }
                        }
                    }
                } else {
                    result.append(CN_NUM_STR[Integer.parseInt(str)]);
                }
            }
            result.append(CN_DATE_STR[i]);
        }
        return result.toString();
    }


    public static void main(String[] args) {
        LocalDateTime testDateTime = LocalDateTime.of(1012, 12, 31, 23, 44, 55);
        String convertDateTime = dateToCnDate(testDateTime);
        System.out.println(("原日期:"+testDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)));
        System.out.println("转换的中文日期:"+convertDateTime);
    }
}

已上传至csdn资源库:https://download.csdn.net/download/shadow_zed/12299030

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值