If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

With all due respect, sjalle, I think you'll find your code produces unexpected results. Calendar months are zero-indexed, so the resulting month of your Date object will be off-by-one. For example, if you use "2005-08-18" as your string, the resulting date from your method will show September as the month (Calendar.SEPTEMBER == 8).

Out of curiosity, why are you trying to parse a SQL date from a String? If you need to insert the date into a database, simply use a PreparedStatement and setDate(). I can't really see why else you might be trying to get a java.sql.Date as opposed to just a java.util.Date...

I forgot about that one, sorry.
Zero-based months is not a new invention, but, add 1 to month and its ok.
I assumed a numeric month.

Adding one is an easy fix, but I'd still recommend parsing the date using DateFormat. If one *does* need to address the individual months, I'd say stick to the Calendar constants (e.g., Calendar.JULY, etc.). These kinds of OBO errors have a tendency to creep into code and be tough to root out.

The database problem is mentioned in the first post in this thread.

Yeah, I'm still not sure of what the actual issue is, as "not working" leaves a lot of possibilities...

I agree that the format parse is the best solution, especially when you know what
kind of date formats you are dealing with, and Calendar is the best class for
processing dates, yes.

My inclination to do a string parse comes from my experience with loading data from
multiple sources into a content management system I made a few years back. I then
had to make a date sniffer due to the insane variety of date "formats" that people like to use.

Since it is a database entry that doesn't work for a date column I think we can
assume a plain SQLException due to an invalid date.....

My inclination to do a string parse comes from my experience with loading data from
multiple sources into a content management system I made a few years back. I then
had to make a date sniffer due to the insane variety of date "formats" that people like to use.

LOL. I hear you. And to be true, if the purpose is simply to transform one date string to another format, parsing a Date object can be overkill.

Since it is a database entry that doesn't work for a date column I think we can assume a plain SQLException due to an invalid date.....

Agreed. It would be nice to know exactly what was wrong with the date; i.e., what was invalid about it.