方法1:可以使用@ControllerAdvice增強Controller
@ControllerAdvice
public class BaseControllerAdvice {
// 初始化綁定
@InitBinder
public void initBinder(WebDataBinder binder) {
//處理表單數據轉換對象異常(Date)
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}
方法2:直接實體類中的字段上加上@DateTimeFormat(pattern="yyyy-MM-dd")
@MappedSuperclass // 配置后,子類可以使用注解
public class BaseEntity implements Serializable {
@Column(name = "CREATE_TIME")
@Temporal(TemporalType.TIMESTAMP) // 實體類會封裝成完整的時間“yyyy-MM-dd hh:MM:ss”的Date類型。
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date creteTime; // 創建時間
}
方法3:直接設置日期數據注解
@RequestMapping("/testDate")
public void testDate(@DateTimeFormat(pattern="yyyy-MM-dd") Date mydate){
System.out.println(mydate+"============");
}