SSM框架中日期类型的转换问题

在SSM框架中,会遇到前台与后台之间的转换文题,其中日期就是一个转换的大问题,在这我就给大家介绍两种我自己经过实验可行的日期转换方法

方法一:在实体类中加注解

 @DateTimeFormat(pattern = "yyyy-MM-dd")
 private Date birthday;

方法二:在控制器(controller)中加入一段绑定代码

@InitBinder
    public void initBinder(WebDataBinder binder){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
   }

猜你喜欢

转载自blog.csdn.net/qq_40978196/article/details/89334623