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

发布了46 篇原创文章 · 获赞 251 · 访问量 90万+

猜你喜欢

转载自blog.csdn.net/shadow_zed/article/details/105289014