項目中,對象屬性日期為Date類型,而前台傳入日期為字符串類型,使用到SpringMVC框架,需進行轉換。
注意使用springMVC框架,前台傳入時間參數,需實現initBinder方法把時間字符串轉換為Date對象。實現了該方法,會自動完成封裝Date類型屬性對象。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); //true:允許輸入空值,false:不能為空值
}
