import org.springframework.format.annotation.DateTimeFormat;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date etd;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date eta;
This code works fine. But I found that an exception is thrown when the time is an empty string, so I add this:
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Now empty string convert to null, but only "yyyy-MM-dd" is used, and the hours and minutes are ignored.
For example, eta, before I add InitBinder, the value is '2020-11-11 11:11:11', after adding it is '2020-11-11 00:00:00'.
How to make spring continue to use the format on the DateTimeFormat annotation?
I found this question but he doesn't seem to use DateTimeFormat : How to handle different date formats in Spring MVC controller?