Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

397
Vistas
LocalDate in form

I have a question about Spring + Thymeleaf date format. I have a simple entity with LocalDate date field. I want to get this date from a user in form and save it to MySQL database. I'm getting such an error:

Failed to convert property value of type java.lang.String to required type java.time.LocalDate for property date; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.time.LocalDate for value 2019-04-30; nested exception is java.time.format.DateTimeParseException: Text 2019-04-30 could not be parsed at index 2

My entity:

@Entity
@Table(name="game")
public class Game{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Transient
    private User gameOwner;
    private LocalDate date;
    private LocalTime time;

    //other fields

Thymeleaf view / form:

<form action="#" th:action="@{/games/addForm}" th:object="${gameForm}" method="post">    
    <p>Date: <input type="date" th:field="*{date}" /></p>
</form>

What's the reason for this problem? Maybe there is the other, better way to storage date?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Problem solved.. I don't know why but changing my template to:

<input type="date" th:value="*{date}" th:field="*{date}" />

and adding @DateTimeFormat(pattern = "yyyy-MM-dd") to entity field solved the problem.

about 4 years ago · Santiago Trujillo Denunciar

0

import org.springframework.format.annotation.DateTimeFormat;

add the following annotation above date.

@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;

put additional curly bracket around date. It will convert date into string

<form action="#" th:action="@{/games/addForm}" th:object="${gameForm}" method="post">    
    <p>Date: <input type="date" th:field="*{{date}}" /></p>
</form>
about 4 years ago · Santiago Trujillo Denunciar

0

I'm unable to reproduce the exact error, but I believe that adding a custom editor for the LocalDate class should fix this. Add this method to your controller:

@InitBinder
protected void initBinder(WebDataBinder binder) {
  binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
    @Override
    public void setAsText(String text) throws IllegalArgumentException{
      setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    }

    @Override
    public String getAsText() throws IllegalArgumentException {
      return DateTimeFormatter.ofPattern("yyyy-MM-dd").format((LocalDate) getValue());
    }
  });
}

It's also possible to add this globally, you'll have to create a ControllerAdvice class and add the method there instead.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda