package com.sbl.pay.subaccount.converter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import com.sbl.pay.subaccount.consumer.SxfQueryReceiver; /** * 日期转化器 * @author ZHANGYUKUN * */ @Component public class DateConverter implements Converter<String,Date> { private static final Logger logger = LoggerFactory.getLogger(SxfQueryReceiver.class); DateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd"); DateFormat ymdhfmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Pattern ymd = Pattern.compile("^\d{4}-\d{1,2}-\d{1,2}$"); Pattern ymdhfm = Pattern.compile("^\d{4}-\d{1,2}-\d{1,2}$"); @Override public Date convert(String dataStr) { dataStr = dataStr.trim(); if ("".equals(dataStr) || dataStr.length()< 10 ) { return null; } try { if( ymd.matcher(dataStr).matches() ) { ymdFormat.parse(dataStr); }else if( ymdhfm.matcher(dataStr).matches() ) { return ymdhfmFormat.parse(dataStr); } }catch (Exception e) { if( logger.isErrorEnabled() ) { logger.error( "日期装换错误,默认返回null" ); } return null; } return null; } }
继承 Converter 实现 转化接口就可以了