【发布时间】:2019-06-20 01:47:33
【问题描述】:
我的问题有很多类似的问题,但我没有找到解决方案。 实体片段:
@DateTimeFormat
private LocalDateTime valid_from;
@DateTimeFormat
private LocalDateTime valid_to;
我的表格格式为 yyyy-MM-dd。我已经尝试过注释 @DateTimeFormat(format="yyyy-MM-dd") 和 ISO。我试过了:
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
webDataBinder.registerCustomEditor(LocalDateTime.class, new CustomDateEditor(dateFormat, true));
}
还有:
@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));
}
但我仍然有绑定错误:
无法将 java.lang.String 类型的属性值转换为属性 valid_from 所需的 java.time.LocalDateTime 类型;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] 值 2019-01- 20;嵌套异常是 java.lang.IllegalArgumentException:值 [2019-01-20] 的解析尝试失败
【问题讨论】:
标签: java spring data-binding