你们平时可能接触到前台传到后台的日期类型,那么你们是如何处理的呢?我先说说我以前是怎么处理的吧!前端
我以前一直使用这种方式来接收前台传进来的日期类型,当初感受挺好用的,一直用一直爽,直到有一天,前端传进来的日期类型变了,原本的格式是yyyy-MM-dd HH:mm:ss,忽然有个数据成了yyyy-MM-dd, 就致使后台报错,类型转换异常。java
代码以下:spring
public class User {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
}
复制代码
错误信息:bash
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'birthday': rejected value [2019-05-27]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2019-05-27'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-05-27]]
复制代码
而后前端就会钉钉滴滴你,服务器内部错误,而后你看了半天,没问题啊,最后实在没办法,是否是你日期格式传的有问题啊,把你传的参数发过来,最后一看果然是这出了问题。这个时候你可能跟我同样把接收的格式改一下,可是万一前端他传进来的日期格式又变了你该怎么办呢?服务器
因此再给你们说第二种处理方式!ide
这个也是我在经历了跟前端各类斗智斗勇以后,学习到的一个方法,比起上面注解的方式,这种方式扩展性更强,并且能够同时处理多种数据格式。学习
咱们先来看一下,若是定义这个转换器ui
@Component
public class DateConverter implements Converter<String, Date> {
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String timeStampFormat = "^\\d+$";
private static final String hDateFormat = "yyyy年MM月dd日 HH:mm:ss";
private static final String hshortDateFormat = "yyyy年MM月dd日";
@Override
public Date convert(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}
value = value.trim();
try {
if (value.contains("-")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
return formatter.parse(value);
} else if (value.matches(timeStampFormat)) {
Long lDate = new Long(value);
return new Date(lDate);
} else if (value.contains("年")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(hDateFormat);
} else {
formatter = new SimpleDateFormat(hshortDateFormat);
}
return formatter.parse(value);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
}
复制代码
上面的年月日格式是我最近的项目中是这种格式,因此是我扩展新加的,因此经过转化器的这种方式,请求进来进来以后,若是参数中有日期格式,那么首先会进入转换器中进行解析,经过上面的代码你们很容易看出,经过传进来的格式判断日期是哪一种格式,而后再进行解析,最后返回一个Date类型,进入咱们的业务代码处理业务。spa
可是若是你传进来的日期格式不符合规范或者说没有定义格式,那么在转换的过程当中,仍是会报错。因此咱们通常最好跟前端规定好某种格式,要么赞成yyyy-MM-dd的格式,要么yyyy年MM月dd日,不要一个接口一个格式,这样处理起来也是很费劲的。code
上周写一个接口,业务代码也不是很复杂,可是代码的量仍是有点东西的,我是真的有点顶不住啊,因此花了一早上把这个接口重构了一下,最后主方法从70多行,减小到了10来行,看起来瞬间舒畅多了,虽然花了一早上,可是仍是挺值得的,因此你们写代码的过程当中,能重构的代码最好仍是重构一下吧!!!