0

I have a jsp file to get data via form, and passing it to a servlet.

<form action="RegistrationProcessing" method="get">
Date of Birth:<input type="text" name="dob">
</form>

This is the servlet file

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws   ServletException, IOException {
String dateofbirth=request.getParameter("dob");
}

I can't change the datatype of dateofbirth variable. It is saying it must be only string, what should i do now to store it as different datatype.I want to store in oracle database where the date format is '01-JAN-2013' , how can i parse to this datatype.

  • possible duplicate of [how can i change the date format in java](http://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java) – Basil Bourque Jan 05 '14 at 23:47

4 Answers4

1

use SimpleDateFormat to convert String to date.Then use java.sql.Date to store it in Database

example

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
SpringLearner
  • 13,546
  • 20
  • 71
  • 113
0

Use SimpleDateFormat() to convert String to Date object.

Do like below.

  java.util.Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");

  java.sql.Date sqlDate= new java.sql.Date(date.getTime());
Prabhakaran Ramaswamy
  • 24,936
  • 10
  • 54
  • 63
0
Date date = new SimpleDateFormat("MMddyyyy").parse(dateofbirth);

You will have to change the "MMddyyyy" format string to fit your dates format.

SpringLearner
  • 13,546
  • 20
  • 71
  • 113
BrianC
  • 1,681
  • 1
  • 16
  • 25
0

You can't get datetype directly,but you can convert a String format date data to a Date format date data.

xinghui
  • 22
  • 3