how to to Inject Custom Formatter in Spring MVC for Date Conversion - Getting TypeMismatchException
I've been banging my head against this for hours. I'm sure I'm missing something obvious here, but I'm deploying to production and I've been banging my head against this for hours... I'm working with an scenario with Spring MVC where I'm trying to inject a custom date formatter into my controller, but I'm getting a `TypeMismatchException` when submitting a form containing a date string. I'm using Spring MVC version 5.3.10 and I have registered the custom formatter as follows: ```java import org.springframework.format.Formatter; import org.springframework.stereotype.Component; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @Component public class CustomDateFormatter implements Formatter<Date> { private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override public Date parse(String text, Locale locale) throws ParseException { return dateFormat.parse(text); } @Override public String print(Date object, Locale locale) { return dateFormat.format(object); } } ``` I've also registered the formatter in my `WebMvcConfigurer` implementation: ```java import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addFormatter(new CustomDateFormatter()); } } ``` In my controller, I have a method that accepts a date parameter: ```java @PostMapping("/submit") public String handleFormSubmission(@RequestParam("date") Date date) { // Process the date return "success"; } ``` When I submit the form with a date in the format `2023-10-15`, the application throws a `TypeMismatchException` with the message: ``` Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2023-10-15' ``` I've confirmed that the date format in the form is correct, and I tried using a different format in the `SimpleDateFormat`, but I still receive the same behavior. I've also ensured that the `CustomDateFormatter` is being scanned by Spring as it is annotated with `@Component`. What could be causing this scenario, and how can I resolve it? Any pointers would be greatly appreciated. My development environment is Linux. Is there a better approach? My development environment is Debian. Thanks, I really appreciate it! I'm working on a REST API that needs to handle this. Thanks in advance! Thanks, I really appreciate it! I recently upgraded to Java latest. What's the correct way to implement this?