-1

I have a drop down menu created in HTML with two dates and a submit button.

Here is the code

<table>
  <tr>
   <td>  Date:  </td>
   <td> <select name="date"> 
      <option> July 02,2012 </option>
      <option> July  06,2012 </option>
      </select> 
   </td>
 </tr>
</table>
<input type="submit" value="Submit">  

The form action is linked to a JSP page. Where I got to create a date object based on the selection from this drop down menu.

What I have done so far

 <tr>
   <td>  Date:  </td>
   <td> <%= request.getParameter("date")%>   </td>
 </tr>  

I know I got to do something like

Date date = new date();

But I do not completely understand where to put this or how do I start?

Mat
  • 202,337
  • 40
  • 393
  • 406
rose petal
  • 21
  • 1
  • 6

1 Answers1

0

request.getParameter("date") returns a String, so you will have to parse that into a Date object. You could use Java's SimpleDateFormat class to do this as follows:

String dateStr = request.getParameter("date");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy");
Date dateObj = dateFormatter.parse(dateStr);

Before you try this though, you will want to make sure that the dates in your dropdown are all consistent in format. In other words, make the spacing and the trailing zeros consistent for all of the <option> elements.

Jake Stoeffler
  • 2,662
  • 24
  • 27
  • Please! create a bean which does this conversion and bind it using use-bean. This code above plus validation of the date string length, format, etc shouldn't be done in scriptlets unless you absolutely must. Also, you will be using these values for something correct? So make sure the values of your options are set to not just the displayed text. – user1288802 Jun 30 '12 at 04:22
  • My apologies -- I figured this would be a good start as the OP is obviously not very familiar with Java/JSP... – Jake Stoeffler Jun 30 '12 at 04:31
  • String date = request.getParameter("date"); SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy"); Date dateObj = dateFormatter.parse(date); User user = new User( webinar, date); UserIO.add(user, path); <%= user.getData()%> I modified my code like this. I have a normal get and set methods. – rose petal Jun 30 '12 at 05:01
  • I figured it out thank you very much. Jstiffs u r da best. thank you. – rose petal Jun 30 '12 at 05:24
  • You're welcome, glad you got it working. I'd appreciate it if you upvoted and selected my answer. – Jake Stoeffler Jul 01 '12 at 05:32