chengf
2026-01-30 b8bf2507bc7b23c90bc6dc71ea2460e277d3800a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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));
    }
 
    public static Double defaultDoubleSplit(Object o) {
        return o == null || o.equals("") ? null : Double.parseDouble(defaultValue(o).split("/")[0]);
    }
 
    public static String defaultValue(Object o, String defaultValue) {
        return o == null ? defaultValue : o.toString();
    }
 
    public static String defaultValueToNull(Object o) {
        return o == null || o.equals("") ? null : o.toString();
    }
 
    public static String appendHyphenToRight(String str) {
        // 1. 判空:null 或 去除首尾空格后为空字符串,都视为"空"
        if (str == null || str.trim().isEmpty()) {
            return str; // 空值直接返回,保持原有状态(null还是空字符串)
        }
        // 2. 非空则右侧拼接"-"
        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;
    }
}