There are many similar questions for my problem but I didn't find resolution. Fragment of entity:
@DateTimeFormat
private LocalDateTime valid_from;
@DateTimeFormat
private LocalDateTime valid_to;
My form has format yyyy-MM-dd. I already tried annotation @DateTimeFormat(format="yyyy-MM-dd") and ISO. I tried with:
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
webDataBinder.registerCustomEditor(LocalDateTime.class, new CustomDateEditor(dateFormat, true));
}
And:
@Converter(autoApply = false)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, String> {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@Override
public String convertToDatabaseColumn(LocalDateTime locDate) {
return (locDate == null ? null : formatter.format(locDate));
}
@Override
public LocalDateTime convertToEntityAttribute(String dateValue) {
return (dateValue == null ? null : LocalDateTime.parse(dateValue, formatter));
}
but I still have binding errors:
Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime for property valid_from; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value 2019-01-20; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-01-20]