Request.getParameter String value passed as date in java

On my JSP page, I have a field that is a date, and when I receive as request.getParameter("dateVal"); gives me

 15-Dec-2012 12:21. 

I would like to pass this value to my database procedure and insert / update into the table. How to pass setDate value with prepareCall to database?

thanks

0
java date oracle10g jsp
source share
3 answers

The first step will be to use SimpleDateFormat to parse it into a fully functional java.util.Date instance in the controller:

 Date date = new SimpleDateFormat("dd-MMM-yyyy HH:mm.", Locale.ENGLISH).parse(dateVal); 

Then you can simply create java.sql.Date around its time in the database layer:

 statement.setDate(1, new java.sql.Date(date.getTime())); 

Not tied to a specific problem, note that java.sql.Date does not remember the temporary part. If you have a DATETIME or TIMESTAMP field in the database and not a DATE field, use setTimestamp() with setTimestamp() instead. Thus, the temporary part will also be saved.

+1
source share

@BalusC answer is perfect. But as an alternative solution, you can use the function provided by the database to convert String to Date during queries. For example (if you are using Oracle),

 to_date(date_in_String, format) 
0
source share

Try the following:

new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(mydate);

-one
source share

All Articles