How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"

Cover Image for How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Hey there! Are you struggling with using LocalDateTime RequestParam in Spring and getting the "Failed to convert String to LocalDateTime" error? 😱 Don't worry, I've got your back! In this blog post, I'll guide you through the common issues and provide you with easy solutions. So, let's get started! 🚀

First things first, make sure you have included the "jackson-datatype-jsr310" dependency in your Spring Boot project. This dependency helps with the serialization and deserialization of Java 8 Date/Time types. You can add it to your Maven project like this:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.7.3</version>
</dependency>

Once you have the dependency configured, let's dive into the issue. In your controller method, you're using a RequestParam with LocalDateTime. Here's an example:

@GetMapping("/test")
public Page<User> get(
    @RequestParam(value = "start", required = false)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) {
    // ...
}

The problem arises when you try to test the endpoint with a URL like this:

/test?start=2016-10-8T00:00

You receive an error message like this:

{
  "timestamp": 1477528408379,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
  "message": "Failed to convert value of type [java.lang.String] to required type [java.time.LocalDateTime]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2016-10-8T00:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2016-10-8T00:00]",
  "path": "/test"
}

This error is thrown because Spring is unable to convert the String value "2016-10-8T00:00" to a LocalDateTime object.

To fix this issue, you need to provide a date format that matches the pattern of your input string. In this case, the pattern is "yyyy-MM-dd'T'HH:mm". Here's how you can modify your controller method:

@GetMapping("/test")
public Page<User> get(
    @RequestParam(value = "start", required = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime start) {
    // ...
}

Now, when you test the endpoint with the same URL, you should no longer encounter the "Failed to convert String to LocalDateTime" error. 🎉

That's it! I hope this guide helped you resolve your issue. Feel free to share this blog post with your friends who might be facing a similar problem. If you have any further questions or need assistance, leave a comment below. Happy coding! 💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello