SpringMVC 表单里有多种日期格式的数据绑定
代码来源于以下三篇博文
[1]: https://blog.csdn.net/dailuwen/article/details/60322864
[2]: https://blog.csdn.net/fsgsggd/article/details/80644012?utm_source=blogxgwz5
[3]: https://blog.csdn.net/u011028777/article/details/72593932
自己写一个DATE数据绑定类;然后在Controller 里面写 InitBinder 方法里面应用;如下代码
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.corba.se.impl.io.TypeMismatchException;
/**
*
* @Program Name : smas.com.jc.smas.util.SpecialDateEditor.java
* @Creation Date : 2018-11-21 上午10:42:44
* @version : v1.00
* @Description : 日期格式化
*
*
*
*
*
**
*/
public class SpecialDateEditor extends PropertyEditorSupport {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void setAsText(String text) throws IllegalArgumentException {
Date date = null;
SimpleDateFormat sdf = null;
try {
if (Pattern.compile("([GMT]|[gmt])").matcher(text).find()) { //Wed Nov 21 2018 08:00:00 GMT+0800(中国标准时间)
sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
try {
text = text.replace("GMT", "").replaceAll("\\(.*\\)", "");
date = sdf.parse(text);
setValue(date);
return;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sdf = getSimpleDateFormat(text);
//防止空数据出错
if(StringUtils.isNotBlank(text)){
date = sdf.parse(text);
}
} catch (ParseException e) {
e.printStackTrace();
}
setValue(date);
}
/**
*
* @Enclosing_Method : getSimpleDateFormat
* @Creation Date : 2018-11-21 下午12:04:07
* @version : v1.00
* @Description : 使用正在表达式匹配正确的格式
*
* @param source
* @return
*
*/
private SimpleDateFormat getSimpleDateFormat(String source) {
SimpleDateFormat sdf = new SimpleDateFormat();
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) { // yyyy-MM-dd
sdf = new SimpleDateFormat("yyyy-MM-dd");
} else if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}-\\d{2}-\\d{2}$", source)) { // yyyy-MM-dd HH-mm-ss
sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
} else if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", source)) { // yyyy-MM-dd HH:mm:ss
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) { // yyyy/MM/dd
sdf = new SimpleDateFormat("yyyy/MM/dd");
} else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2} \\d{2}/\\d{2}/\\d{2}$", source)) { // yyyy/MM/dd HH/mm/ss
sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
} else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) { // yyyyMMdd
sdf = new SimpleDateFormat("yyyyMMdd");
} else if (Pattern.matches("^\\d{4}\\d{2}\\d{2} \\d{2}\\d{2}\\d{2}$", source)) { // yyyyMMdd HHmmss
sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
} else if (Pattern.matches("^\\d{4}\\.\\d{2}\\.\\d{2}$", source)) { // yyyy.MM.dd
sdf = new SimpleDateFormat("yyyy.MM.dd");
} else if (Pattern.matches("^\\d{4}\\.\\d{2}\\.\\d{2} \\d{2}\\.\\d{2}\\.\\d{2}$", source)) { // yyyy.MM.dd HH.mm.ss
sdf = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss");
}else if (Pattern.matches("^\\d{4}-\\d{2}$", source)) { // yyyy-MM-dd
sdf = new SimpleDateFormat("yyyy-MM");
}else{
System.out.println("TypeMismatchException");
throw new TypeMismatchException();
}
return sdf;
}
}
然后在Controller的initBinder 方法里直接引用(我的是一个公共Controller类,其他的Controller类都是继承自他。所以并不需要每个Controller都需要写此方法。否则的话需要每个Controller都需要写此方法)
@InitBinder
public void initBinder(WebDataBinder b) {
b.registerCustomEditor(Date.class, new SpecialDateEditor());
}