spring框架传2019-01-01格式,后台用Date类型接收时错误问题
spring原来传2019/01/01格式的时间,能够正常接收。
如果传参时需要同这种格式:2019-01-01;可以重写重写spring日期转换器。
解决办法:
1、重写spring日期转换器
package com.test.core.customDateEditor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
/**
* 重写spring日期转换器,自定义日期转换器
* 解决spring接收date类型参数的问题
*
*/
public class MyCustomDateEditor extends PropertyEditorSupport {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
/**
*
* Parse the Date from the given text, using the specified DateFormat.
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (!StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
}
else {
try {
setValue(this.dateAdapter(text));
}
catch (Exception ex) {
ex.printStackTrace();
logger.error("出错日志:" + ex.getMessage());
}
}
}
/**
*
* Format the Date as String, using the specified DateFormat.
*/
@Override
public String getAsText() {
Date value = (Date) getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return (value != null ? dateFormat.format(value) : "");
}
/**
*
* 字符串转日期适配方法
*
* @param dateStr
* 日期字符串
*
* @throws FrameworkException
*/
public static Date dateAdapter(String dateStr) throws Exception {
Date date = null;
String temp = dateStr;// 缓存原始数据
if (!(null == dateStr || "".equals(dateStr))) {
// 判断是不是日期字符串,如Wed May 28 08:00:00 CST 2014
if (dateStr.contains("CST")) {
date = new Date(dateStr);
} else {
dateStr = dateStr.replace("年", "-").replace("月", "-")
.replace("日", "").replaceAll("/", "-")
.replaceAll("\\.", "-").trim();
String fm = ""; // 确定日期格式
if (Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}.*")
.matcher(dateStr).matches()) {
fm = "yyyy-MM-dd";
} else if (Pattern
.compile(
"^[0-9]{4}-[0-9]{1}-[0-9]+.*||^[0-9]{4}-[0-9]+-[0-9]{1}.*")
.matcher(dateStr).matches()) {
fm = "yyyy-M-d";
} else if (Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}.*")
.matcher(dateStr).matches()) {
fm = "yy-MM-dd";
} else if (Pattern
.compile(
"^[0-9]{2}-[0-9]{1}-[0-9]+.*||^[0-9]{2}-[0-9]+-[0-9]{1}.*")
.matcher(dateStr).matches()) {
fm = "yy-M-d";
}
// 确定时间格式
if (Pattern.compile(".*[ ][0-9]{2}").matcher(dateStr).matches()) {
fm += " HH";
} else if (Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}")
.matcher(dateStr).matches()) {
fm += " HH:mm";
} else if (Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}")
.matcher(dateStr).matches()) {
fm += " HH:mm:ss";
} else if (Pattern
.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{0,3}")
.matcher(dateStr).matches()) {
fm += " HH:mm:ss:sss";
}
if (!"".equals(fm)) {
try {
date = new SimpleDateFormat(fm).parse(dateStr);
} catch (ParseException e) {
throw new Exception("参数字符串" + dateStr + "无法被转换为日期格式!");
}
}
}
}
return date;
}
}
2、创建BaseController类
package com.tianque.core.base;
import com.tianque.core.customDateEditor.MyCustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import java.util.Date;
public class BaseController {
// WebDataBinder实现将请求request绑定到复杂属性时的请求字符string到属性的转换
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new MyCustomDateEditor());
}
}
3、需要有Date类型的接收参数的,可以继承BaseAction
package com.tianque.core.test.action;
import com.tianque.core.base;
import java.util.Date;
import org.springframework.stereotype.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(value = "/test")
@RestController
public class TestController extends BaseController{
@RequestMapping(value = "test", method = RequestMethod.POST)
@ResponseBody
public String test(Date date) {
// TODO
}
}
这样时间传 2019-01-01 的时候,就可以接收到了。