当前位置:   article > 正文

spring boot 请求参数String类型自动转Date_请求参数字符串转date字段

请求参数字符串转date字段

转载自:http://cxytiandi.com/blog/detail/8101

叙述

今天在post添加数据时,由于接收参数的实体类中的日期是Date类型,导致提交到后台报错。

参数提交上来时是字符串,需要转换成日期类型才可以。

解决

代码实现:

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.springframework.core.convert.converter.Converter;
  5. /**
  6. * 日期转换器,解决在post请求中日期类型参数自动转Date类型
  7. * @author yinjihuan
  8. *
  9. */
  10. public class StringToDateConverter implements Converter<String, Date> {
  11. private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
  12. private static final String shortDateFormat = "yyyy-MM-dd";
  13. private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
  14. private static final String shortDateFormat2 = "yyyy/MM/dd";
  15. @Override
  16. public Date convert(String source) {
  17. if (StringUtils.isBlank(source)) {
  18. return null;
  19. }
  20. source = source.trim();
  21. try {
  22. SimpleDateFormat formatter;
  23. if (source.contains("-")) {
  24. if (source.contains(":")) {
  25. formatter = new SimpleDateFormat(dateFormat);
  26. } else {
  27. formatter = new SimpleDateFormat(shortDateFormat);
  28. }
  29. Date dtDate = formatter.parse(source);
  30. return dtDate;
  31. } else if (source.contains("/")) {
  32. if (source.contains(":")) {
  33. formatter = new SimpleDateFormat(dateFormat2);
  34. } else {
  35. formatter = new SimpleDateFormat(shortDateFormat2);
  36. }
  37. Date dtDate = formatter.parse(source);
  38. return dtDate;
  39. }
  40. } catch (Exception e) {
  41. throw new RuntimeException(String.format("parser %s to Date fail", source));
  42. }
  43. throw new RuntimeException(String.format("parser %s to Date fail", source));
  44. }
  45. }
  1. import javax.annotation.PostConstruct;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.core.convert.support.GenericConversionService;
  5. import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
  6. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
  7. import com.fangjia.amp.common.StringToDateConverter;
  8. @Configuration
  9. public class WebConfigBeans {
  10. @Autowired
  11. private RequestMappingHandlerAdapter handlerAdapter;
  12. /**
  13. * 增加字符串转日期的功能
  14. */
  15. @PostConstruct
  16. public void initEditableValidation() {
  17. ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
  18. if (initializer.getConversionService() != null) {
  19. GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
  20. genericConversionService.addConverter(new StringToDateConverter());
  21. }
  22. }
  23. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/229687
推荐阅读
相关标签
  

闽ICP备14008679号