'How to take the time from Java Thymeleaf and send it to the controller? What is the type of parametar? (JAVA)
I am new to Java spring and i am learning how to make reservations. I have assignment to make a web application that helps clients to make reservation in certain coffee that they choose. I have booking form where there is a input for date ,time and for how many people is the reservation. Here is the input field in HTML where i insert only the time.
<input type="time" id="appt" min="09:00" max="21:00" class="form-control" step="60" name="time">
My question is what type should i make the time parametar in the controller. Here is also example of the format that is passed for time parametar when i insert 12:00 PM to the input field: time=12:00 What should i change in my controller to start working?
@PostMapping( "/book")
public String makeReservation(Model model,
@RequestParam Long objectId,
@RequestParam Integer numPersons,
@RequestParam String date,
@RequestParam String time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate=LocalDate.parse(date,formatter);
LocalDateTime localDateTime = LocalDateTime.parse(time);
return "master-template";
}
Previous i was using java.sql.Time but now i am trying to parse the String time to LocalDateTime but it shows me this error
java.time.format.DateTimeParseException: Text '12:00' could not be parsed at index 0
Solution 1:[1]
Here is the solution how to pass the time while passing it by field input with type=time
@PostMapping("/book")
public String makeReservation(Model model,
@RequestParam Long objectId,
@RequestParam Integer numPersons,
@RequestParam String date,
@RequestParam String time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(date, formatter);
LocalTime localTime = LocalTime.parse(time);
return "redirect:/home";
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | StudentB |
