0

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);
Kris Schouw
  • 352
  • 1
  • 5
  • 21
  • See http://stackoverflow.com/q/3323618/422353 – madth3 Nov 10 '11 at 02:12
  • The answer from that question seems akin to what I've tried, but it hasn't worked. Tomcat throws an illegal argument exception when I try to pass the string into the Date constructor. I would have thought that it could recognize that string format, but I guess not. What's the best way to format the string so that it can be recognized? – Kris Schouw Nov 10 '11 at 02:19

2 Answers2

2

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.

madth3
  • 7,275
  • 12
  • 50
  • 74
  • 1
    +1 but I would suggest using JodaTime library, it, as opposed to SimpleDateFormatter is thread-safe and have more features – Igor Nikolaev Nov 10 '11 at 02:37
  • So would something like `SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d kk:mm:ss.S");`? If so, once I have this format created, do I just format the string with df.format(...) before passing it into the constructor? – Kris Schouw Nov 10 '11 at 02:43
  • 2
    No, you want to `parse` rather than `format`: SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d kk:mm:ss.S"); Date date = df.parse(strDateTime); Timestamp timeStamp = new Timestamp(date.getTime()); – Tim Nov 10 '11 at 03:06
0

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.

Gabriel Graves
  • 1,751
  • 1
  • 22
  • 40
  • Well, I'm actually pulling the date and time from a field on my webpage making use of the jQuery DateTimePicker plugin. I then convert it with JavaScript to an identical format as what is stored in the database, for I thought that was easiest. I then ship it to the servlet with an AJAX call. – Kris Schouw Nov 10 '11 at 02:44
  • _I then convert it with JavaScript to an identical format as what is stored in the database_ - Although it looks like you're doing that, it's not what is really happening. The database doesn't store dates as strings, so you're useing JavaScript to convert it into the same string format that the database converts its dates into. That's probably not as useful as you think it is. – Tim Nov 10 '11 at 03:08