0

Inside a jsp file, I have passed an element of input type="date" and I want to insert that element to the column of type Date inside the database. When I pass this,

<input type="date" class="well well-sm" id="startDate" name="startDate">

and receive it inside the servlet as:

String date=request.getParameter("startDate");
p.setFromDate(date);

When I printed the date, I'm getting 2018-05-24 which is the input I'm passing, which is in the required 'yyyy-MM-dd' format. I'm getting this date inside another class in the below format:

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date ufromdate=sdf1.parse(p.getFromDate());//p.getFromDate()returns a string which has been assigned the value passed from the input element of type date.
java.sql.Date sqlfromdate=new java.sql.Date(ufromdate.getTime());
ps.setDate(5,sqlfromdate );

Here I'm getting ParseException even though I followed the correct pattern given as a reference in other answers written for similar questions in StackOverflow. I'm still getting this exception.

22
  • 5
    "returns a string which has been assigned the value passed from the input element of type date." And what is the actual content of that string? I bet you a tenner it's not in the yyyy-MM-dd format. Commented May 15, 2018 at 18:02
  • usually it is that hidden whitespace. try trimming Commented May 15, 2018 at 18:08
  • 1
    Add the input you're passing in. Also may need to mention your Locale. Please see minimal reproducible example Commented May 15, 2018 at 18:10
  • 1
    Add this line just before calling sdf1.parse: if (!"2018-05-24".equals(p.getFromDate())) throw new RuntimeException("["+p.getFromDate()+"]"); (just for debugging purposes). Commented May 15, 2018 at 18:19
  • 2
    How many lives the holy debugger saved... Commented May 15, 2018 at 19:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.