xuxiuxi
2017-07-31 3be560df41e1287577b751870b0cf95dab9c114c
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
package com.awsle.aibatis.xml.engine.converters.basic;
 
import com.awsle.aibatis.xml.engine.converters.ConversionException;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
public class DateConverter extends AbstractBasicConverter {
 
    private DateFormat dateFormat;
 
    public boolean canConvert(Class type) {
        return type.equals(Date.class);
    }
 
    public DateConverter(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }
 
    public DateConverter() {
        this(new SimpleDateFormat("yyyy-MM-dd HH:mm:ssa"));
    }
 
    protected Object fromString(String str) {
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            throw new ConversionException("Cannot parse date " + str, e);
        }
    }
 
    protected String toString(Object obj) {
        Date date = (Date) obj;
        return dateFormat.format(date);
    }
 
}