0

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 an 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 parameter in the controller.

Here is also example of the format that is passed for time parameter when I insert 12:00 PM to the input field: time=12:00

What should I change in my controller to make it 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";
    }

Previously, I was using java.sql.Time but now I am trying to parse the string time to LocalDateTime but I get this error:

java.time.format.DateTimeParseException: Text '12:00' could not be parsed at index 0

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
StudentB
  • 1
  • 1
  • 1
    What doesn't work? What do you get, please add that information to the question. Also the fact that there is `Date` and `Time` I suspect you are using `java.sql.Time`. Don't use that, stay far from it. Use `javax.time.LocalTime` (and `LocalDate` instead). Finally use an object for the form to bind the parameters instead of individual parameters. – M. Deinum Mar 14 '22 at 12:41
  • If you can, avoid `Date` and `Time`. Those classes are poorly designed and long outdated and were only meant for use with SQL databases. Instead see if `LocalDate` and `LocalTime` work. Both are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 15 '22 at 08:25
  • Hey guys, thank you for your suggestions. You both were right i was using java.sql.Time. I fixed the problem with Date. I changed it to LocaleDate. But still don't know how to fix the problem with geting the time. – StudentB Mar 17 '22 at 13:57

1 Answers1

0

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";
        }
StudentB
  • 1
  • 1