- 直接复制工具类调用你需要转换的方法就行
package com.guodi.bpm.tool.util.formatUtil;
import com.alibaba.excel.util.StringUtils;
import com.guodi.bpm.tool.util.DateUtil;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author lwh
* @description: 数字时间格式化工具类
* @date: 2023/6/29 15:01
*/
public class FormatUtil{
public static void main(String[] args) {
}
/***
* @author lwh
* function: 将数字转化为yyyy年MM月dd日格式和yyyy-MM-dd
* @createDate 2023-6-29 下午15:18:12
* @param time 日期
* @return 转换后的yyyy年MM月dd日格式
*/
@Override
public String toUpper(String time,String type) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dTime = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat dTimes = new SimpleDateFormat("yyyy-MM-dd");
String format = null;
if (time != null && StringUtils.isNotBlank(time)) {
Date parse = null;
try {
parse = df.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = dTimes.format(parse);
format = dTime.format(parse);
}
return format;
}
/***
* @author lwh
* function: 将数字转化为中文大写日期格式和年份中文格式返回
* @createDate 2023-6-29 下午15:33:12
* @param time 日期
* @return 转换后的中文大写日期格式 二○二三年六月二十六日
*/
public String toCnUpper(String time) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String timeFormat = null;
if (time != null && StringUtils.isNotBlank(time)) {
Date format = null;
try {
format = df.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
//时间转大写
timeFormat = dataToUpper(format);
}
return timeFormat;
}
/***
* @author lwh
* function: 将日期按类型返回 年 月 日 (数字与中文)
* @createDate 2023-7-3 上午9:44
* @param time 日期
* @return 将日期按类型返回【int】
*/
public Object toFormatSpecificDate(String time,String type){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
if (time != null && StringUtils.isNotBlank(time)) {
try {
Date format = df.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(format);
// 获取年份、月份和日期
return calendar.get(Calendar.YEAR);
return calendar.get(Calendar.MONTH)+1;
return calendar.get(Calendar.DAY_OF_MONTH);
return numToUpper(calendar.get(Calendar.YEAR));
return monthToUppder(calendar.get(Calendar.MONTH)+1);
return dayToUppder(calendar.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
/***
* @author lwh
* function: 将数字按类型返回中文 中文数字,格式:贰零零染
* @createDate 2023-7-3 上午10:44
* @param num 数字
* @return 将日期按类型返回【string】
*/
public String toCnUpperNum(int num,String type){
if (type.equals(FormatEnum.NUMBERCN2.getValue())){
String toUpperNum = numCnToUpper(num);
return toUpperNum;
}else if (type.equals(FormatEnum.NUMBERCN.getValue())){
String convert = getNumberStr(num);
return convert;
}
return null;
}
/***
* @author lwh
* function: 将数字按类型返回中文 中文数字,格式:贰仟零柒圆整
* @createDate 2023-7-4 下午16:52
* @param num 数字
* @return 将日期按类型返回【string】
*/
public String toCnUpperNum(String num){
BigDecimal bigDecimal = new BigDecimal(num);
String convert = convert(bigDecimal);
return convert;
}
//转中文金额 begin ====================================================================================================
private static final String[] CN_NUMERIC = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] CN_UNIT = {"圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"};
public static String convert(BigDecimal amount) {
if (amount.compareTo(BigDecimal.ZERO) == 0) {
return "零元整";
}
StringBuilder result = new StringBuilder();
// 转换为字符串,并去除小数点后多余的0
String strAmount = amount.stripTrailingZeros().toPlainString();
int dotIndex = strAmount.indexOf(".");
// 整数部分
String integerPart = "";
if (dotIndex > -1) {
integerPart = strAmount.substring(0, dotIndex);
} else {
integerPart = strAmount;
}
int length = integerPart.length();
// 是否遇到连续的0
boolean isZero = false;
for (int i = 0; i < length; i++) {
// 获取当前数字的值
int digit = Integer.parseInt(integerPart.substring(i, i + 1));
int unitIndex = length - i - 1; // 数字所在的单位位置
boolean isLastUnit = unitIndex == 0; // 是否为最后一个单位
// 处理连续的0
if (digit == 0) {
isZero = true;
// 若当前数字是最后一个单位对应的数字,并且前面还有非0数字,则追加单位
if (isLastUnit && !result.toString().endsWith("零")) {
result.append(CN_UNIT[unitIndex]);
}
} else {
// 处理非0数字
if (isZero) {
result.append("零");
isZero = false;
}
result.append(CN_NUMERIC[digit]).append(CN_UNIT[unitIndex]);
}
}
// 处理小数部分
if (dotIndex > -1) {
String decimalPart = strAmount.substring(dotIndex + 1);
int decLength = decimalPart.length();
if (decLength == 1) {
int digit = Integer.parseInt(decimalPart);
if (digit != 0) {
result.append(CN_NUMERIC[digit]).append("角");
}
} else if (decLength == 2) {
int digit1 = Integer.parseInt(decimalPart.substring(0, 1));
int digit2 = Integer.parseInt(decimalPart.substring(1, 2));
if (digit1 != 0) {
result.append(CN_NUMERIC[digit1]).append("角");
}
if (digit2 != 0) {
result.append(CN_NUMERIC[digit2]).append("分");
}
}
}
return result.append("整").toString();
}
//转中文金额 end ====================================================================================================
//日期转化为大小写 begin ====================================================================================================
public static String dataToUpper(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
int year = ca.get(Calendar.YEAR);
int month = ca.get(Calendar.MONTH) + 1;
int day = ca.get(Calendar.DAY_OF_MONTH);
return numToUpper(year) + "年" + monthToUppder(month) + "月" + dayToUppder(day) + "日";
}
/***
* function: 月转化为大写
* @createDate 2010-5-27 上午10:41:42
* @param month 月份
* @return 返回转换后大写月份
*/
public static String monthToUppder(int month) {
if (month < 10) {
return numToUpper(month);
} else if (month == 10) {
return "十";
} else {
return "十" + numToUpper(month - 10);
}
}
/***
* function: 日转化为大写
* @createDate 2010-5-27 上午10:43:32
* @param day 日期
* @return 转换大写的日期格式
*/
public static String dayToUppder(int day) {
if (day < 20) {
return monthToUppder(day);
} else {
char[] str = String.valueOf(day).toCharArray();
if (str[1] == '0') {
return numToUpper(Integer.parseInt(str[0] + "")) + "十";
} else {
return numToUpper(Integer.parseInt(str[0] + "")) + "十" + numToUpper(Integer.parseInt(str[1] + ""));
}
}
}
//日期转化为大小写 end ====================================================================================================
//转中文数字 begin ====================================================================================================
/***
* function: 将数字转化为大写
* @createDate 2010-5-27 上午10:28:12
* @param num 数字
* @return 转换后的大写数字
*/
public static String numToUpper(int num) {
// String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
// String u[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
String u[] = {"○", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
char[] str = String.valueOf(num).toCharArray();
String rstr = "";
for (int i = 0; i < str.length; i++) {
rstr = rstr + u[Integer.parseInt(str[i] + "")];
}
return rstr;
}
/***
* function: 将数字转化为大写
* @createDate 2010-5-27 上午10:28:12
* @param num 数字
* @return 转换后的大写数字
*/
public static String numCnToUpper(int num) {
String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
char[] str = String.valueOf(num).toCharArray();
String rstr = "";
for (int i = 0; i < str.length; i++) {
rstr = rstr + u[Integer.parseInt(str[i] + "")];
}
return rstr;
}
private static String[] NUMBER1 = {"零", "一", "两", "三", "四", "五", "六", "七", "八", "九"};
private static String[] NUMBER2 = {"零", "十", "百", "千", "万", "亿"};
/**
* @param num
* @return
* @Author:lwh
* @Description:将数字转化为大写
*/
public static String getNumberStr(int num) {
if (num < 0) {
return "";
}
if (num == 0) {
return NUMBER1[0];
}
int split = 10000;
int y = num / (split * split);
int w = (num / split) % split;
int g = num % split;
StringBuffer sb = new StringBuffer();
//亿
if (y > 0) {
sb.append(getNumberStr1000(y));
sb.append(NUMBER2[5]);
}
//万
if (w > 999) {
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
} else {
if (w > 0) {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
}
}
//万以下
if (g > 0) {
if (w != 0) {
if (g > 999) {
sb.append(getNumberStr1000(g));
} else {
sb.append(NUMBER2[0]);
sb.append(getNumberStr1000(g));
}
} else {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(g));
}
}
return sb.toString();
}
/**
* @param num
* @return
* @Description:对万以下的数字进行大小写转化
*/
private static String getNumberStr1000 (int num) {
if (num > 9999 || num < 0) {
return "";
}
int q = num / 1000;
int b = (num / 100) % 10;
int s = (num / 10) % 10;
int g = num % 10;
StringBuffer sb = new StringBuffer();
//千
if (q > 0) {
sb.append(NUMBER1[q]);
sb.append(NUMBER2[3]);
}
//百
if (b > 0) {
sb.append(NUMBER1[b]);
sb.append(NUMBER2[2]);
} else {
if (q != 0) {
sb.append(NUMBER2[0]);
}
}
//十
if (s > 0) {
sb.append(NUMBER1[s]);
sb.append(NUMBER2[1]);
} else {
if (b != 0) {
sb.append(NUMBER2[0]);
}
}
//个
if (g > 0) {
sb.append(NUMBER1[g]);
}
return sb.toString();
}
//转中文数字 end ====================================================================================================
}