我有一个 jsp 文件,用于通过表单获取数据,并将其传递给 servlet。
<form action="RegistrationProcessing" method="get">
Date of Birth:<input type="text" name="dob">
</form>
这是 servlet 文件
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String dateofbirth=request.getParameter("dob");
}
我无法更改 dateofbirth 变量的数据类型。它说它必须只是字符串,我现在应该做什么来将其存储为不同的数据类型。我想存储在oracle数据库中,其中日期格式为 '01-JAN-2013' ,我如何解析为这种数据类型。
最佳答案
使用SimpleDateFormat
将String
转换为date
。然后使用java.sql.Date
将其存储在数据库
示例
String DateStr="2013-10-31T19:00:00Z";// date String
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr); //converted String to date in the desired format
java.sql.Date d1 = new java.sql.Date(d.getTime());//formatted to java.sql.date format to store in database
关于java - 如何在 servlet 内将文本表单数据从 jsp 转换为日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917860/