im trying to validate a @DateTimeFormat(pattern="HH:mm") generated with JQuery Timepicker on Spring 4.3, and i have a problem when i send the data object to the controller, this generate a exception.
Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property schedule.shChildTime; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "00:00"
The attribute on data base is:
sh_child_time time without time zone NOT NULL DEFAULT '00:00:00'::time without time zone
Hibernate reverse engineering make the class mapping and i add the notation, the attributes Schedule class is:
private boolean shHappen;
private char shStatus;
private short shOrder;
@DateTimeFormat(pattern = "HH:mm")
private Date shChildTime;
private int[][] shOrderMatrix;
The controller is receiving a Schedule object across the ModelAttribute:
@RequestMapping(value = "/schedules/new", method = RequestMethod.POST)
public String saveSchedule(@ModelAttribute("persistDto")Schedule schedule, BindingResult result,Model model,Locale locale,HttpServletRequest request) {}
The form object is, th:object="${persistDto}" and the input with thymeleaf is:
<input th:field="*{schedule.shChildTime}" type="text" class="form-control" id="inputShChildTime" th:placeholder="#{schedule.hour.real}"/>
UPDATE
The solution is add a InitBinder on the controller with the data date format that i send on the model and format the data in the fields on the front end:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
Example data format: "2017-01-01 12:00:12"