zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
package com.shlanbao.tzsc.base.editor;
 
import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.util.Date;
 
import org.springframework.util.StringUtils;
 
import com.shlanbao.tzsc.utils.tools.DateUtil;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
 
public class TimeStampPropertyEditor extends PropertyEditorSupport {
    private String pattern = "yyyy-MM-dd HH:mm:ss";
 
    private String[] formater_pattern = new String[] {"yyyy-MM-dd HH:mm:ss" };
 
    public TimeStampPropertyEditor() {
    }
 
    public TimeStampPropertyEditor(String pattern) {
        this.pattern = pattern;
    }
 
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isEmpty(text)) {
            setValue(null);
        } else {
            try {
                Date date = DateUtil.formatStringToDate(text, formater_pattern);
                Timestamp timestamp = new Timestamp(date.getTime());
                // 设置转换完的值
                setValue(timestamp);
            } catch (ParseException e) {
                e.printStackTrace();
                setValue(null);
            }
        }
 
    }
 
    @Override
    public String getAsText() {
        // TODO Auto-generated method stub
        // 获取model的值
        Timestamp value = (Timestamp) getValue();
        if (value == null) {
            return "";
        } else {
            try {
                Date date = new Date(value.getTime());
                String str = DateUtil.formatDateToString(date, pattern);
                return str;
            } catch (Exception e) {
                return "";
            }
        }
 
    }
 
}