0

I have a form with a input type date to bind with a propery localDatetime in a bean. It is always getting null in the controller. If I change LocalDateTime by Date everything is ok. Any idea what is happening?

Bean :

@Entity
@Table(name = "visits")
public class Visit {

....
@Column(name = "visit_date")
    @DateTimeFormat(iso = ISO.DATE_TIME)
    private LocalDateTime date;
..
}

View:

<tr>
    <td><label>Date:</label></td>
    <td><form:input type="date" path="date" /></td>
</tr>

Controller:

@PostMapping("/saveVisit")
public String saveVisit(@ModelAttribute("visit") Visit theVisit, BindingResult result) {

    System.out.println("visit " + theVisit.toString());

    // visitService.saveVisit(theVisit);

    // send over to our form
    return "redirect:/pet/showListVisits?petId=" + theVisit.getPet().getPet_id();
}

Result : visit Visit [visit_id=0, pet=Pet [pet_id=1, owner=null, name=null, type=null], date=null, issue=]

Antonio Diaz
  • 133
  • 1
  • 1
  • 14
  • can you please provide some sample records stored in the database, just to know the format in which it's stored in the database. – Sandeep Kamath Apr 09 '20 at 06:29
  • Hi, the input type date work witht he patter "yyyy-MM-dd" . Anyway, here you go the sample : 2020-04-09 00:00:00. The field in the databse is timestamp type – Antonio Diaz Apr 09 '20 at 07:33

2 Answers2

1

I've figured out the problem, I was using imput type=date but the bean was localDateTime. I changed it by LocalDate and currently is working.

Thanks

Antonio Diaz
  • 133
  • 1
  • 1
  • 14
  • Yes, that would have been the correct way, that is why I was asking about a record in the database. when you have only the date with "yyyy-MM-dd" format stored in database, the best is to use LocalDate. – Sandeep Kamath Apr 09 '20 at 21:41
0

You can check the Lu55 answer here https://stackoverflow.com/a/47934151/13258364

Maybe it helps you to found a solution to your problem

Ner
  • 104
  • 6