Reputation: 352
I am basically trying to convert a string similar to this one: "2011-11-9 18:24:12.3
" into a format that I can insert into a database table claiming this particular column to be of the "datetime" type. I figured I could do something along the lines of preparedStatement.setTimestamp(...)
, but I can't seem to get a Timestamp created correctly. Can anyone suggest the easiest way to convert a string like the one above to either a Timestamp or some other type which is compatible with MySQL's "datetime" type? Any help would be appreciated.
Thus far, I've tried doing something like this:
String strDateTime = "2011-11-9 18:24:12.3";
Timestamp timeStamp = new Timestamp((new Date(strDateTime)).getTime());
preparedStatement.setTimestamp(1, timeStamp);
Upvotes: 0
Views: 925
Reputation: 7344
Ah, so, the problem is most likely not with the datetime of mysql but with the date.
The Date(Strings s)
constructor is currently deprecated and is recommended to use a SimpleDateFormat which would let you use any format you want.
Upvotes: 2
Reputation: 1785
Have you tried splitting and making it into a format you can use? This assumes that this is in the same format all the time. Create a new string that takes apart a split of the old string and rearranges it in a useful way. If this is a string provided by the end-user, you may have to validate it first and check to make sure it is usable.
Upvotes: 0