Handling parse exception to a date format from a date picker

Tasy Androes :

Here I am trying to handle the start date of my booking system as well as the end date using LocalDate and date formatted. I have done some search on the error I am getting, however, they don't deal with similar issues. Like here I need to get the user input using request.getParameter.

        //Getting input values from jsp from 
        String cruiseName = request.getParameter("cruiseName");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MMM/yyyy");

        LocalDate startDate =(LocalDate) LocalDate.parse(request.getParameter("startDate"),df);

        LocalDate endDate =(LocalDate) LocalDate.parse(request.getParameter("endDate"),df);

The error message:

Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.time.format.DateTimeParseException: Text '03/10/2020' could not be parsed at index 3
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)** 

Root Cause:
java.time.format.DateTimeParseException: Text '03/10/2020' could not be parsed at index 3
    java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
    java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
    java.base/java.time.LocalDate.parse(LocalDate.java:428)
Dmitry Pisklov :

The format you are trying to parse is different from the format you get, either because there's incorrect format asked from the user, or because you used the wrong format to parse.

You are trying to parse dd/MMM/yyyy format which would look like 03/Oct/2020, but the format you get is either dd/MM/yyyy or MM/dd/yyyy, from the error message you get 03/10/2020 which can be either. You need to check what the expected input is and adjust your parsing code, or ask user for particular format.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291133&siteId=1