ZhuDongming
2019-08-15 9902b8ccf2d4a7a297189a39c5843f52a83c7315
src/main/java/com/moral/common/convert/StringToDateConverter.java
@@ -9,34 +9,36 @@
import java.util.Date;
/**
 * 字符串转日期的转换器
 * @author byshome
 * @version $Id: StringToDateConverter.java, v 0.1 2015年9月24日 下午7:19:41 byshome Exp $
 *
 * 字符串转日期的转换器:
 * @author: fengxiang
 * @date: 2018/7/30 14:40
 */
public class StringToDateConverter implements Converter<String, Date> {
      Logger log = Logger.getLogger(StringToDateConverter.class);
    private static   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);
    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);
            if(StringUtils.isNullOrEmpty(source)){
                return  null;
            }
            if(StringUtils.isNumericZidai(source)) {
                source = source.length() == 10 ? source+"000" : source;
                return new Date(Long.parseLong(source));
            } else {
                return  simpleDateFormat.parse(source);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }