java将日期转换为汉字(2022-12-01转换为二零二二年十二月一号)工具类

DateConvertor

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConvertor {
    public static void main(String[] args)throws Exception {
        Long day = 1000L * 60 * 60 * 24;
        Long month = day * 30;
        Long year = day * 365;
        Date date = new Date();

//        for (int i = 0; i < 10000; i++) {
//            long time = date.getTime();
//            time = time + day * i;
            System.out.println(formatStr(new Date()));
//        }
    }

    /**
     * @param date 时间
     * @return 汉字年月日
     */
    public static String formatStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        String format = sdf.format(date);
        String[] split = format.split("-");
        //2020-7-18 ==> 二零二零年七月十八日
        String year = split[0];
        String month = split[1];
        String day = split[2];
        String strYear = getStrYear(year);
        String strMonth = getStrMonth(month);
        String strDay = getStrDay(day);
        String formatStr = strYear + "年" + strMonth + "月" + strDay + "号";

        return formatStr;
    }

    /**
     * @param day 数字日 13
     * @return 汉字日 13==>十三
     */
    public static String getStrDay(String day) {
        int dayLength = day.length();
        String strDay = "";
        if (dayLength < 2) {
            strDay = numToStr(day);
            return strDay;
        } else {
            if (day.startsWith("1")) {
                if (day.equals("10")) {
                    return "十";
                } else {
                    String substring = day.substring(1);
                    strDay = "十" + numToStr(substring);
                    return strDay;
                }
            } else if (day.startsWith("2")) {
                if (day.equals("20")) {
                    return "二十";
                } else {
                    String substring = day.substring(1);
                    strDay = "二十" + numToStr(substring);
                    return strDay;
                }
            } else if (day.startsWith("3")) {
                if (day.equals("30")) {
                    return "三十";
                } else {
                    String substring = day.substring(1);
                    strDay = "三十" + numToStr(substring);
                    return strDay;
                }
            }
        }
        return "";
    }


    /**
     * @param month 数字月 7
     * @return 中文月 7==>七
     */
    public static String getStrMonth(String month) {
        String strMonth = numToStr(month);
        return strMonth;
    }

    /**
     * @param year 数字年 2020
     * @return 汉字年 二零二零
     */
    public static String getStrYear(String year) {
        int length = year.length();
        String strYear = "";
        for (int i = 0; i < length; i++) {
            String substring = year.substring(i, i + 1);
            strYear = strYear + numToStr(substring);
        }
        return strYear;
    }

    /**
     * @param nun 0~9 ==>零~九
     * @return
     */
    public static String numToStr(String nun) {
        if (nun.equals("0")) {
            return "零";
        } else if (nun.equals("1")) {
            return "一";
        } else if (nun.equals("2")) {
            return "二";
        } else if (nun.equals("3")) {
            return "三";
        } else if (nun.equals("4")) {
            return "四";
        } else if (nun.equals("5")) {
            return "五";
        } else if (nun.equals("6")) {
            return "六";
        } else if (nun.equals("7")) {
            return "七";
        } else if (nun.equals("8")) {
            return "八";
        } else if (nun.equals("9")) {
            return "九";
        } else if (nun.equals("10")) {
            return "十";
        } else if (nun.equals("11")) {
            return "十一";
        } else if (nun.equals("12")) {
            return "十二";
        }
        return "";
    }
}

执行main方法得到结果如下 (可以更改为将时分秒转换为汉字)
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名落魄的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值