String str = "13/06/2011";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = (Date)formatter.parse(str);
3 Answers
I guess that your Date class is actually a java.sql.Date.
8 Comments
Vijay Nag
If I remove the cast, it gives me compilation error. Type mismatch: cannot convert from java.util.Date to java.sql.Date
Howard
@vijay you should import
java.util.Date instead of java.sql.Date.Vijay Nag
Thanks Howard. I did that and now I am not getting the cast exception. But now I am getting the below exception. Java.text.ParseException: Unparseable date: 12/21/2011
Howard
@vijay Do you now have another format (other than posted in your question)? It looks like because with above format it should work (nevertheless you seem to have MM/dd/yyyy now instead of dd/MM/yyyy).
Vijay Nag
This is my exact code .....SimpleDateFormat dateFormat = new SimpleDateFormat("MM:dd:yyyy"); Date convertedDate; try { convertedDate = dateFormat.parse(dob); dateFormat.parseObject(dob); } catch (ParseException e) { e.printStackTrace(); } and DOB is in this format : 12/21/2011
|
What does your import statement say? Are you importing some other class (for example java.sql.Date) by accident? What does the compiler say when you remove the class cast (which should not be there)?
DateFormat.parse() returns an instance of java.util.Date and not java.sql.Date.
In order to convert from java.util.Date to java.sql.Date, I do the following:
java.util.Date fromDate = df.parse(fromdate1);
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime());
1 Comment
Chad
After about an hour of looking, I found this and it works great.