lihang
2023-04-25 3fade6d3b27f5666672bb3af610020367f790bda
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.vci.ubcs.starter.web.toolmodel;
 
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.VciDateUtil;
import org.springblade.core.tool.utils.StringUtil;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class DateConverter {
    private DateFormat dateFormat;
    private int exactDateLength;
    private Date value;
 
    public DateConverter() {
    }
 
    public DateFormat getDateFormat() {
        return this.dateFormat;
    }
 
    public void setDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }
 
    public int getExactDateLength() {
        return this.exactDateLength;
    }
 
    public void setExactDateLength(int exactDateLength) {
        this.exactDateLength = exactDateLength;
    }
 
    public Date getValue() {
        return this.value;
    }
 
    public void setValue(Date value) {
        this.value = value;
    }
 
    public void setAsText(String text) throws VciBaseException {
        if (StringUtil.isBlank(text)) {
            this.setValue((Date)null);
        } else {
            text = text.trim();
            text = text.replace("年", "-").replace("月", "-").replace("日", "").replace("时", ":").replace("分", ":");
            String pattern = "\\d{2,4}([^\\d]?)\\d{1,2}\\1\\d{1,2}( \\d{1,2}([^\\d])\\d{1,2})?";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(text);
            if (!m.find()) {
                throw new VciBaseException("不是合格的时间格式字符串,{0}", new String[]{text});
            }
 
            String dateSplit = m.group(1);
            String timeSplit = m.group(3);
            String formateStr = String.format("yyyy%sMM%sdd", dateSplit, dateSplit);
            String yearMD;
            if (StringUtil.isNotBlank(timeSplit)) {
                yearMD = text.substring(text.indexOf(" "));
                String[] split = yearMD.split(timeSplit);
                if (split.length == 2) {
                    formateStr = formateStr + String.format(" HH%smm", timeSplit);
                }
 
                if (split.length > 2) {
                    formateStr = formateStr + String.format(" HH%smm%sss", timeSplit, timeSplit);
                }
 
                if (yearMD.contains(".")) {
                    formateStr = formateStr + ".SSS";
                }
            }
 
            yearMD = text.contains(" ") ? text.substring(0, text.indexOf(" ")) : text;
            if (StringUtil.isNotBlank(dateSplit)) {
                String year = text.substring(0, text.indexOf(dateSplit));
                if (StringUtil.isNotBlank(year) && year.length() == 2) {
                    formateStr = formateStr.replace("yyyy", "yy");
                }
 
                String[] split = yearMD.split(dateSplit);
                if (split.length == 2) {
                    formateStr = formateStr.replace("dd", "");
                }
            } else if (!text.matches("^[0-9]*$")) {
                if (text.length() == 5) {
                    formateStr = formateStr.replace("yyyyMMdd", "yy" + text.substring(2, 3) + "MM");
                }
 
                if (text.length() == 7) {
                    formateStr = formateStr.replace("yyyyMMdd", "yyyy" + text.substring(4, 5) + "MM");
                }
            } else if (text.length() == 2 && text.matches("^[0-9]*$")) {
                formateStr = "yy";
            } else if (text.length() == 4 && text.matches("^[0-9]*$")) {
                formateStr = "yyyy";
            } else if (text.length() == 6 && text.matches("^[0-9]*$")) {
                formateStr = "yyyyMM";
            } else if (StringUtil.isNotBlank(yearMD) && yearMD.length() < 6) {
                formateStr = formateStr.replace("yyyy", "yy");
            }
 
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formateStr);
 
            try {
                this.setValue(simpleDateFormat.parse(text));
            } catch (ParseException var13) {
                formateStr = formateStr.replace("yyyy", "yy");
                simpleDateFormat = new SimpleDateFormat(formateStr);
 
                try {
                    this.setValue(simpleDateFormat.parse(text));
                } catch (ParseException var12) {
                    if (text.length() == 2 && text.matches("^[0-9]*$")) {
                        formateStr = "yy";
                    }
 
                    throw new VciBaseException("不能格式化日期: {0}", new String[]{text}, var13);
                }
            }
        }
 
    }
 
    private String fillNano(String text) {
        String nano = text.substring(text.lastIndexOf(".") + 1);
        if (nano.length() < 3) {
            for(int i = 0; i < 3 - nano.length(); ++i) {
                nano = nano + "0";
            }
        }
 
        return text.substring(0, text.lastIndexOf(".")) + "." + nano;
    }
 
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>() {
            {
                this.add("2020-11-12 12:12:03");
                this.add("2020-1-2 2:2:3");
                this.add("2020-11-12 12:12:03.232");
                this.add("2020/11/12 12:12:03");
                this.add("2020/1/2 2:2:3");
                this.add("2020/11/12 12:12:03.232");
                this.add("20/11/12 12:12:03");
                this.add("20/1/2 2:2:3");
                this.add("20/11/12 12:12:03.232");
                this.add("2020");
                this.add("2020-11");
                this.add("202011");
                this.add("12:02:12");
                this.add("2:2:3");
                this.add("20201112");
                this.add("202012");
                this.add("202012");
                this.add("2020年11月12日 12时12分03秒");
                this.add("2020年1月2日 2:2:3");
                this.add("2020年11月12日 12时12分03秒232毫秒");
            }
        };
        list.stream().forEach((t) -> {
            try {
                DateConverter dateConverter = new DateConverter();
                dateConverter.setAsText(t);
                System.out.println("转换前:" + t + "; 转换后:" + VciDateUtil.date2Str(dateConverter.getValue(), "yyyy-MM-dd HH:mm:ss.SSS"));
            } catch (Throwable var2) {
                var2.printStackTrace();
            }
 
        });
    }
 
    public String getAsText(String dateFormat) {
        Date value = this.getValue();
        if (StringUtil.isEmpty(dateFormat)) {
            dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
        }
 
        return value != null ? (new SimpleDateFormat(dateFormat)).format(value) : "";
    }
}