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.
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();
}
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.
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.