fengxiang
2018-05-04 4a41bd4e105385b5460e5a81c8b67e5f701a262b
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
package com.moral.common.convert;
 
import com.moral.common.exception.BusinessException;
import com.moral.common.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 字符串转日期的转换器
 * @author byshome
 * @version $Id: StringToDateConverter.java, v 0.1 2015年9月24日 下午7:19:41 byshome Exp $
 */
public class StringToDateConverter implements Converter<String, Date> {
      Logger log = Logger.getLogger(StringToDateConverter.class);
    private static final String dateFormat      = "yyyy-MM-dd HH:mm:ss";
//    private static final String shortDateFormat = "yyyy-MM-dd";
      private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
    /** 
     * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
     */
    @Override
    public Date convert(String source) {
          if(StringUtils.isNullOrEmpty(source)){
              throw new BusinessException(String.format("parser %s to Date fail", source));
          }
          if("null".equals(source)){
              return  null;
          }
          if(StringUtils.isNumericZidai(source)){
              return  new Date(Long.parseLong(source));
          }
        try {
            return  simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }
 
}