1

So to start off, this code is working when I use @DateTimeFormat(pattern = "HH:mm"). But as soon as I change it to @DateTimeFormat(pattern = "hh:mm") it stops working. I'm pretty new to spring so I'm not sure where the problem could be coming from. All I want to do is save a time to my DB.

The Error:

Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalTime] for value '12:00';

This is the object that I am mapping to the form:

@Entity
@Table(name = "Schedule")
@NoArgsConstructor
@Getter
@Setter
public class Schedule {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @DateTimeFormat(pattern = "hh:mm")
    private LocalTime startTime;
    @DateTimeFormat(pattern = "hh:mm")
    private LocalTime endTime;
    @Column(name = "days")
    private String days;

    public Schedule(LocalTime startTime, LocalTime endTime, String days) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.days = days;
    }
}

And this is my controller:

@Controller
@RequestMapping("/schedules")
@AllArgsConstructor
public class WebScheduleController {
    
    ScheduleService scheduleService;

    @GetMapping
    public String getSchedules(Model model) {
        List<Schedule> schedules = scheduleService.getSchedules();
        model.addAttribute("scheduleList", schedules);
        model.addAttribute("schedule", new Schedule());
        return "schedules";
    }

    @PostMapping
    public String createSchedule(@ModelAttribute("schedule") Schedule schedule,Model model ){

        scheduleService.createSchedule(schedule);

        return "redirect:/schedules/";
    }
    

}

And this is the HTML form:

<form method="post" th:action="@{/schedules}" th:object="${schedule}" class="col card p-3 mb-5">
    <div class="form-group">
        <label for="startTime">Start time</label>
        <input id="startTime" placeholder="Enter start time" required type="time" th:field="*{startTime}"/>
    </div>
    <div class="form-group">
        <label for="endTime">End time</label>
        <input id="endTime" placeholder="Enter end time" required type="time" th:field="*{endTime}" />
    </div>
    <div class="form-group">
        <label for="days">Days</label>
        <input id="days" placeholder="Enter days" required type="text" th:field="*{days}" />
    </div>
    <input type="submit" class="btn btn-primary" value="Create Schedule">
</form>
Peter Shepherd
  • 168
  • 1
  • 10
  • 2
    Lower case `hh` is for hour within AM or PM, so Jackson doesn’t know whether you intended 12:00 AM or 12:00 PM. Upper case `HH` is for hour of day, so 12:00 is 12 noon unambiguously, therefore it works. Related: [DateTimeParseException: Text '2019-06-07 12:18:16' could not be parsed](https://stackoverflow.com/questions/56500476/datetimeparseexception-text-2019-06-07-121816-could-not-be-parsed). – Ole V.V. May 05 '22 at 15:59
  • @OleV.V. Thanks for the help. So I understand that but when I try the format of `hh:mm a` I still get the same error. In the HTML form where I enter the time, there is an option to choose between AM/PM so I'm really not sure what else I can try. – Peter Shepherd May 05 '22 at 16:19

1 Answers1

1

have you try to remove am/pm? maybe this String am/pm also trying to be part of time when sending data. which you database dont allow String in Date.

  • 1
    But if you wish to have am/pm make it string and put some validation to it –  May 05 '22 at 16:48
  • 1
    or separate the am/pm in database and also to html form as String –  May 05 '22 at 16:51
  • Thanks for the help! I just now realized that the html time input will always return a string in the format of `HH:mm` regardless of what times I pick. For example: if I pick `03:00 PM` the input will send `15:00` back. – Peter Shepherd May 05 '22 at 17:01
  • Or maybe since your method createSchedule is String its giving it String –  May 05 '22 at 17:27