0

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"

Camilo C
  • 1
  • 4

1 Answers1

0

May be your problem is same mentioned in below link: Unable to convert String to Date by requestBody in spring

Or you can write converter. You can go through below link http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/htmlsingle/#core-convert

Community
  • 1
  • 1