ludc
2025-01-16 5203081b68e3a8dc139d1807b2f8774e4a00a82a
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.vci.starter.web.toolmodel;
 
import com.vci.starter.web.constant.RegExpConstant;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.util.VciDateUtil;
import org.apache.commons.lang3.StringUtils;
 
import java.beans.PropertyEditorSupport;
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;
 
/**
 * 时间处理,前台json传输过来的内容的时候
 * @author weidy
 */
public class DateConverter extends PropertyEditorSupport {
 
    /**
     *时间格式
     */
    private DateFormat dateFormat;
 
     /**
     *时间内容的长度
     */
    private  int exactDateLength;
 
    /**
     * 具体的值
     */
    private Date value;
 
    public DateFormat getDateFormat() {
        return dateFormat;
    }
 
    public void setDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }
 
    public int getExactDateLength() {
        return exactDateLength;
    }
 
    public void setExactDateLength(int exactDateLength) {
        this.exactDateLength = exactDateLength;
    }
 
    public Date getValue() {
        return value;
    }
 
    public void setValue(Date value) {
        this.value = value;
    }
 
    /**
     *将字符串转换为日期格式
     * @param text 字符串
     * @throws VciBaseException 转换出错的时候抛出异常
     */
    public void setAsText(String text) throws VciBaseException {
        if (StringUtils.isBlank(text)) {
            setValue(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()){
                //说明是符合要求的
                String dateSplit = m.group(1);
                //中间的空格是第二部分
                String timeSplit = m.group(3);
                String formateStr = String.format("yyyy%sMM%sdd",dateSplit,dateSplit);
                if(StringUtils.isNotBlank(timeSplit)){
                    //有时间
                    String timeStr = text.substring(text.indexOf(" "));
                    String[] split = timeStr.split(timeSplit);
                    if(split.length == 2){
                        formateStr += String.format(" HH%smm",timeSplit);
                    }
                    if(split.length>2){
                        formateStr += String.format(" HH%smm%sss",timeSplit,timeSplit);
                    }
                    if(timeStr.contains(".")){
                        //毫秒
                        formateStr += ".SSS";
                    }
                }
                String yearMD = text.contains(" ")?text.substring(0, text.indexOf(" ")):text;
                if(StringUtils.isNotBlank(dateSplit)){
                    //说明有日期的分割符
                    String year = text.substring(0, text.indexOf(dateSplit));
                    if(StringUtils.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(RegExpConstant.NUMBER)){
                        //说明还是有符号,但是不符合要求而已
                        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(RegExpConstant.NUMBER)) {
                            formateStr = "yy";
                        } else if (text.length() == 4 && text.matches(RegExpConstant.NUMBER)) {
                            formateStr = "yyyy";
                        } else if (text.length() == 6 && text.matches(RegExpConstant.NUMBER)) {
                            formateStr = "yyyyMM";
                        } else {
                            if (StringUtils.isNotBlank(yearMD) && yearMD.length() < 6) {
                                formateStr = formateStr.replace("yyyy", "yy");
                            }
                        }
                    }
                }
                //只有时间,我们是没办法转换的
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formateStr);
                try {
                    setValue(simpleDateFormat.parse(text));
                } catch (ParseException e) {
                    //是看看短年份
                    formateStr= formateStr.replace("yyyy","yy");
                    simpleDateFormat = new SimpleDateFormat(formateStr);
                    try {
                        setValue(simpleDateFormat.parse(text));
                    } catch (ParseException ex) {
                        //如果只有日期
                        if(text.length() == 2 && text.matches(RegExpConstant.NUMBER)){
                            formateStr = "yy";
                        }
                        throw new VciBaseException("不能格式化日期: {0}", new String[]{text}, e);
                    }
                }
            }else{
                throw new VciBaseException("不是合格的时间格式字符串,{0}", new String[]{text});
            }
//            if(text.contains("年") || text.contains("月") || text.contains("日")
//            ||text.contains("时") || text.contains("分") || text.contains("秒") || text.contains("毫秒")
//            ||text.contains("周") || text.contains("礼拜") || text.contains("天")){
//                //1. 只有年
//                if(text.indexOf("年") == text.length()-1){
//                    this.exactDateLength = text.length();
//                    if(text.length() ==3) {
//                        this.dateFormat = new SimpleDateFormat("yy年");
//                    }
//                }
//            }
//            if(text.contains("/")) {
//                if ( text.trim().length() == 19) {
//                    this.exactDateLength = 19;
//                    this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//                } else if (text.indexOf(".") > -1 && text.length() > 20) {
//                    this.exactDateLength = 23;
//                    text = fillNano(text);
//                    this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
//                } else if (text.length() == 17) {
//                    this.exactDateLength = 17;
//                    this.dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
//                } else if ( text.indexOf(".") > -1 && text.length() > 17) {
//                    this.exactDateLength = 21;
//                    text = fillNano(text);
//                    this.dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss.SSS");
//                } else if (text.length() == 10) {
//                    this.exactDateLength = 10;
//                    this.dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                } else if (text.length() >=8 && text.length() < 10 ) {
//                    //2018/1/1  或者18/01/01,18/1/1
//                    if(text.substring(0,text.indexOf("/")).length() == 4){
//                        this.dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                    }else{
//                        this.dateFormat = new SimpleDateFormat("yy/MM/dd");
//                    }
//                    this.exactDateLength = text.length();
//                }else{
//                    this.dateFormat = new SimpleDateFormat("yy/MM/dd");
//                    this.exactDateLength = text.length();
//                }
//            }else if(text.contains("-")) {
//                if (text.length() == 19) {
//                    this.exactDateLength = 19;
//                    this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                } else if ( text.indexOf(".") > -1 && text.length() > 20) {
//                    this.exactDateLength = 23;
//                    text = fillNano(text);
//                    this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//                } else if (text.length() == 17) {
//                    this.exactDateLength = 17;
//                    this.dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
//                } else if ( text.length() == 8) {
//                    this.exactDateLength = 8;
//                    this.dateFormat = new SimpleDateFormat("yy-MM-dd");
//                } else if (text.length() == 10) {
//                    this.exactDateLength = 10;
//                    this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                }else if (text.length() >=8 && text.length() < 10 ) {
//                    //2018-1-1  或者18-01-01,18/1/1
//                    if(text.substring(0,text.indexOf("-")).length() == 4){
//                        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                    }else{
//                        this.dateFormat = new SimpleDateFormat("yy-MM-dd");
//                    }
//                    this.exactDateLength = text.length();
//                }else{
//                    this.dateFormat = new SimpleDateFormat("yy-MM-dd");
//                    this.exactDateLength = text.length();
//                }//先弄这么多,不够再添加
//            }else if(text.matches(RegExpConstant.NUMBER)){
//
//            }else{
//                this.exactDateLength = 19;
//                this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            }
//            //if ((text != null) && (this.exactDateLength >= 0)&& (text.length() != this.exactDateLength)) {
//            //    throw new IllegalArgumentException("不能初始化时间,因为内容不到" + this.exactDateLength + "长度");
//        //    }
//            try {
//                setValue(VciDateUtil.str2Date(VciDateUtil.date2Str(this.dateFormat.parse(text),VciDateUtil.DateTimeMillFormat),VciDateUtil.DateTimeMillFormat));
//                //无论界面是什么格式,我们都转化为"yyyy-MM-dd HH:mm:ss.SSS"这样可以统一后台的内容
//            } catch (ParseException ex) {
//                throw new VciBaseException("不能格式化日期: "+ ex.getMessage(), new Object[0],ex);
//            } catch (Exception e) {
//                throw new VciBaseException("不能格式化日期: "+ e.getMessage(), new Object[0], e);
//            }
        }
    }
 
    /**
     * 抛弃毫秒
     * @param text 字符串
     * @return 没有毫秒的时间值
     */
    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 += "0";
            }
        }
        return text.substring(0,text.lastIndexOf(".")) + "." + nano;
    }
 
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(){{
            add("2020-11-12 12:12:03");
            add("2020-1-2 2:2:3");
            add("2020-11-12 12:12:03.232");
            add("2020/11/12 12:12:03");
            add("2020/1/2 2:2:3");
            add("2020/11/12 12:12:03.232");
            add("20/11/12 12:12:03");
            add("20/1/2 2:2:3");
            add("20/11/12 12:12:03.232");
            add("2020");
            add("2020-11");
            add("202011");
            add("12:02:12");
            add("2:2:3");
            add("20201112");
            add("202012");
            add("202012");
            add("2020年11月12日 12时12分03秒");
            add("2020年1月2日 2:2:3");
            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(), VciDateUtil.DateTimeMillFormat));
            }catch (Throwable e){
                e.printStackTrace();
            }
        });
 
    }
 
    /**
     * 获取日期转换为字符串的值
     * @return 字符串
     */
    public String getAsText(String dateFormat) {
        Date value = (Date) getValue();
        if(StringUtils.isEmpty(dateFormat)){
            dateFormat= VciDateUtil.DateTimeMillFormat;
        }
        return value != null ? new SimpleDateFormat(dateFormat).format(value) : "";
    }
}