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.

Sathesh
  • 486
  • 2
  • 12
  • 29
  • 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,738
  • 20
  • 78
  • 116
  • I successfully parsed to that format , but i want to store in oracle database where the date format is '01-JAN-2013' , how can i parse to this datatype. – Sathesh Jan 04 '14 at 07:02
  • @SatheshBm update your question with your reuirement and I will also update my answer – SpringLearner Jan 04 '14 at 07:03
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
  • 25,706
  • 10
  • 57
  • 64
0
Date date = new SimpleDateFormat("MMddyyyy").parse(dateofbirth);

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

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
BrianC
  • 1,793
  • 1
  • 18
  • 26
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