//一个参数的情况
public static String toChar(Object o) {
if (o == null) {
return null;
}
String returnValue = String.valueOf(o);
return StringUtils.isEmpty(returnValue) ? null : returnValue;
}
//两个参数的情况
public staticString toChar(T data, String format) { if (data == null) { return null; } if (data instanceof Date date) { return formatDate(date, format); } return formatFm(toChar(data), format); } //日期格式 public static String formatDate(Date data, String format) { LocalDateTime localDateTime; localDateTime = LocalDateTime.ofInstant((data).toInstant(), ZoneId.systemDefault()); Locale locale = Locale.JAPANESE; //Year String year = String.valueOf(localDateTime.getYear()); //Month e.g. Nov String month_short = localDateTime.getMonth().getDisplayName(TextStyle.SHORT, locale); //Month e.g. 11 String month_num = String.valueOf(localDateTime.getMonthValue() > 9 ? localDateTime.getMonthValue() : "0" + localDateTime.getMonthValue()); //月の日,1~31 String dayOfMonth = String.valueOf(localDateTime.getDayOfMonth() > 9 ? localDateTime.getDayOfMonth() : "0" + localDateTime.getDayOfMonth()); //HH24 String hour24 = String.valueOf(localDateTime.getHour() > 9 ? localDateTime.getHour() : "0" + localDateTime.getHour()); //HH12 int hour12_temp = localDateTime.getHour(); if (localDateTime.getHour() > 12) { hour12_temp -= 12; } String hour12 = String.valueOf(hour12_temp > 9 ? hour12_temp : "0" + hour12_temp); //minute String minute = String.valueOf(localDateTime.getMinute() > 9 ? localDateTime.getMinute() : "0" + localDateTime.getMinute()); //second String second = String.valueOf(localDateTime.getSecond() > 9 ? localDateTime.getSecond() : "0" + localDateTime.getSecond()); String date_format = format .replaceAll("(?i)YYYY", year) .replaceAll("(?i)YY", year.substring(2)) .replaceAll("MON", month_short.toUpperCase()) .replaceAll("Mon", month_short) .replaceAll("mon", month_short.toLowerCase()) .replaceAll("(?i)MM", month_num) .replaceAll("(?i)DD", dayOfMonth) .replaceAll("(?i)HH24", hour24) .replaceAll("(?i)HH12", hour12) .replaceAll("(?i)HH", hour12) .replaceAll("(?i)MI", minute) .replaceAll("(?i)SS", second); return date_format; } //数字格式 public static String formatFm(String data, String format) { format = format.toUpperCase(); String format_str = format.replace("FM", ""); BigDecimal bigDecimal = new BigDecimal(data); String pattern = ""; if (format_str.startsWith("0")) { pattern = format_str.replace("9", "0"); } else if (format_str.contains("0.") && format_str.split("\.")[1].endsWith("0")) { pattern = format_str.split("\.")[0].replace("9", "#") + "." + format_str.split("\.")[1].replace("9", "0"); } else { pattern = format_str.replace("9", "#"); } DecimalFormat decimalFormat = new DecimalFormat(pattern); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); String data_str = decimalFormat.format(bigDecimal); //超出最大位后显示# if (data_str.length() > format_str.length()) { String result = ""; for (int i = 0; i <= format_str.length(); i++) { result += "#"; } return result; }
return data_str; }



