This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
How can we insert a formatted Date object into a table.
Table's date column is of data type : Date
format required is : MM/DD/YYYY HH:MM:ss a
I have formatted current time and getting the output properly in string.
Now how can I have it stored in Date object.
parse method of SimpleDateFormat converts it into again same without format.
java begineer wrote:I have formatted current time and getting the output properly in string.
Dates are just dates and have no format. They only have a format when they are converted into a string -- which there is absolutely no need to do when inserting into a Date column. Just leave it as a Date.
My guess is that you are not properly using PreparedStatement, and that you are using Statement and trying to format a SQL statement by converting the Date into a string. If so, wrong, wrong, wrong.
Use a PreparedStatement, and add the date value as a Date, not a String, using the setDate() method.