经常用的Spring 日期格式化
1.使用 jackson 注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”)
- 在controller 初始化时 控制 页面传入的String 类型时间转 date
/**
- 初始化 控制页面传入String 类型时间转 date类型
- @param binder
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}