| | |
| | | package com.java110.dto.importData; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.YearMonth; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.time.format.ResolverStyle; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.Calendar; |
| | | import java.util.Locale; |
| | | |
| | | public class Vtil { |
| | | public static String defaultValue(Object o) { |
| | | return o == null ? "" : o.toString(); |
| | | } |
| | | |
| | | public static String defaultValueToNumber(Object obj) { |
| | | // 1. 处理null值 |
| | | if (obj == null) { |
| | | return ""; |
| | | } |
| | | |
| | | // 2. 将对象转为字符串(兼容各种类型) |
| | | String originalStr = String.valueOf(obj); |
| | | |
| | | // 3. 使用正则表达式替换所有非数字字符 |
| | | // \\D 匹配所有非数字字符(等价于 [^0-9]) |
| | | String numericStr = originalStr.replaceAll("\\D", ""); |
| | | |
| | | // 4. 返回处理后的纯数字字符串 |
| | | return numericStr; |
| | | } |
| | | |
| | | public static Double defaultDouble(Object o) { |
| | | return o == null || o.equals("") ? null : Double.parseDouble(defaultValue(o)); |
| | |
| | | return str + "-"; |
| | | } |
| | | |
| | | public static String defaultValueToDate(Object o) { |
| | | // 空值直接返回null(原有逻辑) |
| | | if (o == null || o.toString().isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | try { |
| | | // 尝试将对象转换为数字(兼容Excel日期序列号,如44747) |
| | | double excelDateNum = Double.parseDouble(o.toString()); |
| | | |
| | | // Excel 1900日期系统基准(修正闰日bug,实际基准是1899-12-30) |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.set(1899, 11, 30, 0, 0, 0); // 月份从0开始,11代表12月 |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | |
| | | // 计算对应的实际日期 |
| | | calendar.add(Calendar.DAY_OF_MONTH, (int) excelDateNum); |
| | | |
| | | // 格式化为yyyy-MM-dd的日期字符串 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime()); |
| | | } catch (NumberFormatException e) { |
| | | // 非数字类型,执行原有逻辑返回toString() |
| | | return o.toString(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public static String defaultValueToDate(Object o, String state) { |
| | | // 空值直接返回null(原有逻辑) |
| | | if (o == null || o.toString().isEmpty() || o.toString().equals("0")) { |
| | | return null; |
| | | } |
| | | |
| | | String input = o.toString().trim(); |
| | | // 定义年月格式的解析器 |
| | | DateTimeFormatter ymFormatter = DateTimeFormatter.ofPattern("yyyy/M") |
| | | .withLocale(Locale.CHINA) |
| | | .withResolverStyle(ResolverStyle.LENIENT); // 宽松解析,兼容各种合法格式 |
| | | |
| | | try { |
| | | // 先解析为YearMonth(专门处理年月的类,不会缺少日期维度) |
| | | YearMonth yearMonth = YearMonth.parse(input, ymFormatter); |
| | | |
| | | // 根据state参数处理日期 |
| | | if ("start".equals(state)) { |
| | | // state为start时返回当月第一天 |
| | | LocalDate firstDay = yearMonth.atDay(1); |
| | | return firstDay.toString(); |
| | | } else if ("end".equals(state)) { |
| | | // state为end时返回当月最后一天 |
| | | LocalDate lastDay = yearMonth.atEndOfMonth(); |
| | | return lastDay.toString(); |
| | | } |
| | | } catch (DateTimeParseException e) { |
| | | // 不是20xx/01格式,忽略解析异常,执行原有逻辑 |
| | | } |
| | | |
| | | // 非目标格式或state不匹配时,返回原始字符串(原有逻辑) |
| | | return input; |
| | | } |
| | | } |