-1

I am trying to convert the String to java.sql.Date. This string I am getting from my jsp page using request.getparameter(date); which will return string and now I am trying to convert this String to Java.util.Date and then converting to java.SQL.Date.
For this I am using the below code

DateFormat format=new SimpleDateFormat("MM-dd-yyyy");
java.util.Date parsed = new Date(0);
try {
    System.out.println("inside try block");
    format.setLenient(false);
    parsed = format.parse(dob);

    System.out.println("parsed date inside try block"+parsed);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

But the statement parsed=format.parse(dob) is not executing.

hg8
  • 1,082
  • 2
  • 15
  • 28
Alekhya
  • 9
  • 4

3 Answers3

2

Try the below code

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString = "07/06/2013";

        try {

            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

Source

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

After the line parsed = format.parse(dob);

you have to add the below line to convert it to java.sql.date format.

java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());

If the string is like "12201430" then you have to use formatter like new SimpleDateFormat("MMyyyydd"),same way for other scenarios also else you will get a ParseException.

robin
  • 1,893
  • 1
  • 18
  • 38
1

You can get this done by using this sample code

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                        nowdate="12-30-2014"
                        Date YourResult=sdf.parse(nowdate);
                        java.sql.Date todayssqldate=new java.sql.Date(YourResult.getTime());