Spring Boot Date conversion from Form

Truica Sorin :

I'm having a few issues with getting a date from a controller because I don't understand what's the right format for it to work.

Controller:

@RequestMapping(value = "/findFlights", method = RequestMethod.POST)
    public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to,
            @RequestParam("departureDate") @DateTimeFormat(pattern = "YYYY-MM-DD") LocalDate departureDate, Model model) {}

Form:

<form th:action="@{/findFlights}" method="POST">
        From:<input type="text" name="from" required/>
        To:<input type="text" name="to" required/>
        Departure Date:<input type="date" name="departureDate"  required />
        <input type="submit" value="Search"/>
</form>

When I submit the form it always gives me an error no matter what the format of the date is :( Here is the error:

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; 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.LocalDate] for value '2018-11-05'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-11-05]

If I specify the @DateTimeFormat annotation I thought that the conversion would be done automatically.

Karol Dowbecki :

In YYYY-MM-DD pattern Y is a week year and D is day in a year as per SimpleDateFormat javadoc. For standard dates the pattern should be yyyy-MM-dd:

@DateTimeFormat(pattern = "yyyy-MM-dd")

or if you want to use DateTimeFormat.ISO enum:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there any way to call function automatically from spring boot, at given date time fetched from PostgreSQL DB table?

Date timezone with Spring boot and Jackson

Spring Boot Multiple Authentication Providers from a Single Login Form Entry Point

Spring Boot How to check if encoded password from db is matching with password from a form before an update

Calling external api from spring boot with multipart/form-data

deploy project from spring boot to tomcat, error in post form

Spring Boot + Spring Data JPA query from java.util.Date type using only the month

spring boot rest decrement on day from date

Spring boot GET datatype conversion not working

Spring Boot : Getting Null inputs from Form Submission

Spring Boot H2 data conversion

SQL Server Date Conversion from Date to NVARCHAR

Date Conversion from mysql to php

Conversion of Date from .Net to Angularjs

Spring MVC form value conversion

Spring automatic type conversion is not working for string to Date

R - Date conversion from factor

form validation not performed, spring boot

Date conversion from string is not valid

Spring Boot doc is out of date

Spring Boot - Hibernate - Import Date from SQL Query

BigQuery Date Conversion From String

Date conversion from string not happening

How can I get an input date from a form in a controller in spring?

Conversion from a formatted date to a variable

Spring Boot & Mustache formatting date

Spring Boot Application Properties Variable Conversion/Matching when retrieved from ENV variable?

Spring Boot Wrong date returned

Snowflake date conversion from varchar

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive