1. 程式人生 > >spring mvc 接受Date型別資料,使用轉換

spring mvc 接受Date型別資料,使用轉換

在controller中新增這個方法
    /**
     * TODO:初始化接收的日期型別不然Date接收不到String型別
     * @param binder
     */
    @InitBinder
    public void dateBinder(WebDataBinder binder) {
        //The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Create a new CustomDateEditor
        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
        //Register it as custom editor for the Date type
        binder.registerCustomEditor(Date.class, editor);
    }