Explain Codes LogoExplain Codes Logo

How to accept Date params in a GET request to Spring MVC Controller?

java
date-format
datetime-format
spring-mvc
Alex KataevbyAlex Kataev·Feb 14, 2025
TLDR

Put to use @RequestParam with @DateTimeFormat in your Spring MVC Controller method to interpret Date parameters from GET requests with ease:

@GetMapping("/getByDate") public String fetchByDate(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) { // Drake meme: LocalDate? YES return "We've got it. Received: " + date; }

This neat piece ensures LocalDate params are correctly translated from a standard ISO-formatted date (yyyy-MM-dd) delivered via the request.

Using uniform date format

Ensure date format consistency throughout your application. For global settings, drop this line in application.properties:

spring.mvc.date-format=yyyy-MM-dd

Approximately guarantees that no date format mismatch will occur across your application.

Customizing date format

If you come across some non-standard date format, just tweak the pattern in @DateTimeFormat and "Voila!":

@GetMapping("/alienDate") public String fetchByAlienDateFormat(@RequestParam @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate date) { //Because we don't discriminate any date formats at all! return "Alien spotted! Received: " + date; }

Be mindful of syntax nightmares or formatting mismatches. They don't play nice with others.

Time zone considerations

If your app is a globe-trotter, time zone handling becomes essential. Standardize on UTC and only convert to local time zones when required.

Stringing along the dates

Sometimes, string parameters offer more control when LocalDate throws a fit:

@GetMapping("/stringyDate") public String fetchByStringDate(@RequestParam String dateString) { LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd")); //String to date, wow! What's next? Winter to summer? return "Et voilà! Parsed : " + date; }

Remember to catch parsing exceptions. Connection lost in translation?

Mapping GET requests for different endpoints

Map a GET request using @GetMapping or the jack of all trades @RequestMapping annotation:

@RequestMapping(value = "/getIt", method = RequestMethod.GET) public String processGetOrd(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) { //Return of the GET. return "Handled the return of the GET with date: " + date; }

Stress testing your controllers

It's vital to test your controller methods to ensure they handle date parameters like a boss. Find your formatting woes and bugs before they find you.

Custom SQL queries with correct date formats

If your persistence layer is picky with date formats, implement custom SQL queries with @Query and keep formatting in check:

@Query("SELECT e FROM Event e WHERE e.date = :date") List<Event> findEventsByDate(@Param("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date);

Error handling and edge cases

Remember, reality is stranger than fiction. Consider edge cases such as leap years and invalid dates. Proper validation logic stops unwanted surprises and boosts reliability.