相关文章推荐
踏实的胡萝卜  ·  Sql ...·  昨天    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime

Ask Question

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));
@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]

In this form I need only date. But in other cases I will need also time. My column in database is timestamp not text. I prefer LocalDateTime rather than sql Date api. – GarryMoveOut Jan 26, 2019 at 17:50

You can add custom Serializer and Deserializer to accomplish what you want to do. Define a serializer like below.

public class MyDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    @Override
    public DateTime deserialize(final JsonParser jsonParser,
                                final DeserializationContext deserializationContext) throws IOException {
        String dateStr = null;
        String timeStr = null;
        String fieldName = null;
        dateStr = jsonParser.getValueAsString();
        if (dateStr != null) {
            return LocalDateTime.parse(dateStr, formatter);
        return null;

You can write a deserializer as below.

public class MyDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    @Override
    public void serialize(final DateTime dateTime,
                          final JsonGenerator jsonGenerator, 
                          final SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(formatter.print(dateTime));

Now you can annotate your datetime fields as below.

@JsonSerialize(using = MyDateTimeSerializer.class)
@JsonDeserialize(using = MyDateTimeDeserializer.class)
private LocalDateTime valid_to;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.