How to set date field in HTML form and how can i retrieve the date in servlets?

I need to set a date field in my html form. After submitting the form the request goes to the servlet and tha data will be stored in database. In servlets how can i retrieve the date field? Please help me.

-renu

Answers


You'll get it out of the HttpRequest parameters:

DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
String dateAsString = httpRequest.getParameter("form-name-for-the-date-here");
Date dateAsObject = dateFormatter.parse(dateAsString);

If you're using Spring you should use their data binding API.


The above answer is good. If you want to change your format at database level use this format.

In PreparedStatement:

  • For MySQL TIMESTAMP use to_timestamp( ? ,'dd-mm-yyyy %h:%i:%s') format.
  • For Oracle TIMESTAMP use to_date(dateAsString ,'DD-MM-RR HH24:MI:SS') format.
  • For MySQL DATE use to_timestamp( ? ,'dd-mm-yyyy') format.
  • For Oracle DATE use to_date(? ,'DD-MM-RR') format.

Set dateAsString in PreparedStatement.

String dateAsString = httpRequest.getParameter("form-name-for-the-date-here");

Need Your Help

Result of expression '$.cookie' [undefined] is not a function

javascript jquery jquery-ui jquery-mobile cordova

Hi all in my mobile application to pass variable in between various pages without ajax I am using cookie. I am setting cookie like :

Next textbox line

c# math textbox

I have a multi-line textbox of sequences, which the game will play one after the other. For example, the textbox may contain this: