SpringBoot传参设置全局Timestamp和Date日期转换类
我的另一篇日期实用博文《Jdk8最全日期转换类,String转各种日期,各种日期转String》
1、设置全局String转换到Timestamp转换类
import org.apache.commons.lang.StringUtils; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.regex.Pattern; @Component public class StringToTimestampConverter implements Converter<String, Timestamp> { // 这里的<String, Timestamp>表示从string转换到timestamp static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS"; static final String REG_YYYY_MM_DD_HH_MM_SS_SSS = "[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[ ]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{3}"; static final String YYYY_MM_DD_HH_MM_SS_S = "yyyy-MM-dd HH:mm:ss.S"; static final String REG_YYYY_MM_DD_HH_MM_SS_S = "[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[ ]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{1}"; static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; static final String REG_YYYY_MM_DD_HH_MM_SS = "[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[ ]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}"; static final String YYYY_MM_DD = "yyyy-MM-dd"; static final String REG_YYYY_MM_DD = "[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}"; static final String REG_NUMBER = "[0-9]{1,}"; @Override public Timestamp convert(String timeStr) { if(StringUtils.isBlank(timeStr)) { return null; } timeStr = timeStr.trim(); try { DateTimeFormatter formatter; if(Pattern.matches(StringToTimestampConverter.REG_YYYY_MM_DD_HH_MM_SS_SSS, timeStr)){ formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SSS); } else if(Pattern.matches(StringToTimestampConverter.REG_YYYY_MM_DD_HH_MM_SS_S, timeStr)){ formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_S); } else if(Pattern.matches(StringToTimestampConverter.REG_YYYY_MM_DD_HH_MM_SS, timeStr)) { formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS); } else if(Pattern.matches(StringToTimestampConverter.REG_YYYY_MM_DD, timeStr)) { formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD); LocalDate localDate = LocalDate.parse(timeStr, formatter); Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); Timestamp timestamp = Timestamp.from(instant); return timestamp; } else if(Pattern.matches(StringToTimestampConverter.REG_NUMBER, timeStr)) { Long lDate = new Long(timeStr); return new Timestamp(lDate); } else { throw new RuntimeException(String.format("parser %s to Date fail", timeStr)); } LocalDateTime localDateTime = LocalDateTime.parse(timeStr, formatter); Timestamp timestamp = Timestamp.valueOf(localDateTime); return timestamp; } catch (Exception e) { throw new RuntimeException(String.format("parser %s to Date fail", timeStr)); } } }
2、设置全局String转换到Date转换类
import org.apache.commons.lang.StringUtils; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.regex.Pattern; @Component public class StringToUtilDateConverter implements Converter<String, Date> { // 这里的<String, Date>表示从string转换到Date static final String YYYY_MM_DD = "yyyy-MM-dd"; static final String REG_YYYY_MM_DD = "[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}"; static final String REG_NUMBER = "[0-9]{1,}"; @Override public Date convert(String timeStr) { if(StringUtils.isBlank(timeStr)) { return null; } timeStr = timeStr.trim(); try { DateTimeFormatter formatter; if(Pattern.matches(StringToUtilDateConverter.REG_YYYY_MM_DD, timeStr)) { formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD); } else if(Pattern.matches(StringToUtilDateConverter.REG_NUMBER, timeStr)) { Long lDate = new Long(timeStr); return new Timestamp(lDate); } else { throw new RuntimeException(String.format("parser %s to Date fail", timeStr)); } LocalDate localDate = LocalDate.parse(timeStr, formatter); Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); return date; } catch (Exception e) { throw new RuntimeException(String.format("parser %s to Date fail", timeStr)); } } }
3、重要!最后一步,把转换类注册到spring容器
import com.myapp.util.StringToTimestampConverter; import com.myapp.util.StringToUtilDateConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import javax.annotation.PostConstruct; @Configuration public class MyappConfigTimestampConverter { @Autowired private RequestMappingHandlerAdapter handlerAdapter; @Autowired private StringToTimestampConverter stringToTimestampConverter; @Autowired private StringToUtilDateConverter stringToUtilDateConverter; /** * converts a string to java.sql.Timestamp and other Date */ @PostConstruct public void initEditableAvlidation() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer(); if(initializer.getConversionService()!=null) { GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService(); genericConversionService.addConverter(stringToTimestampConverter); genericConversionService.addConverter(stringToUtilDateConverter); } } }
end.