request.getParameter String值传递给java的日期 [英] request.getParameter String value pass as date to java
问题描述
在我的JSP页面中,我有一个字段是日期,当我收到 request.getParameter(dateVal);
给我
In my JSP page I have a field which is date and when I getting as request.getParameter("dateVal");
gives me
15-Dec-2012 12:21.
我想将此值传递给我的数据库过程并将其插入/更新到表中。
如何将值作为 setDate
使用 prepareCall
传递给数据库?
I would like to pass this value to my database procedure and insert/update into table.
How can I pass the value as setDate
using prepareCall
to database?
谢谢
推荐答案
第一步将使用 SimpleDateFormat
将其解析为一个完整的 java.util.Date
控制器中的实例:
First step would be using SimpleDateFormat
to parse it to a fullworthy java.util.Date
instance in the controller:
Date date = new SimpleDateFormat("dd-MMM-yyyy HH:mm.", Locale.ENGLISH).parse(dateVal);
然后你可以创建一个 java.sql.Date
层:
Then you can just create a java.sql.Date
around its time in the database layer:
statement.setDate(1, new java.sql.Date(date.getTime()));
无关具体问题请注意, java.sql.Date
不记得时间部分。如果您实际上在DB中实际有一个 DATETIME
或 TIMESTAMP
字段,而不是 DATE
字段,然后改为使用 setTimestamp()
与 java.sql.Timestamp
。这样,时间部分也将被存储。
Unrelated to the concrete problem, please note that java.sql.Date
doesn't remember the time part. If you have actually a DATETIME
or TIMESTAMP
field in the DB and not a DATE
field, then rather use setTimestamp()
with a java.sql.Timestamp
instead. This way the time part will also be stored.
这篇关于request.getParameter String值传递给java的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!