日期格式化
项目结构
Controller代码
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/dateformat")
@ResponseBody
public String dateformat(Date date) {
System.out.println(date);
return "{'info':'dateformat'}";
}
}
在Controller中加入以下方法
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}