Date Conversion

Hi all I am sure that this is a quite simple task, and I am just not thinking of the right thing. I want to convert a string which will have the format of "dd-mmm-yyyy" into an sql date so that I can run it against a database. The only thing that has come to mind is to switch around the year and the day and convert the mmm into mm and use the Date(int int int) constructor, but that has been deprecated.

Can someone suggest something that I am obviously missing ?

thanks in advance.

[504 byte] By [Aknibbsa] at [2007-11-26 13:56:53]
# 1
I'd suggest using java.text.SimpleDateFormat to get a java.util.Date. From that you can get the value as a number of milliseconds, and using that you can construct a java.sql.Date.
paulcwa at 2007-7-8 1:36:40 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks it worked beautifully. For those who may search for this in the future here is what I used:

(dateToday is a string containing today'sdate

sqlEnd is a java.sql.Date to search in the database

*Note* pay special attention to the format took me a few minutes to realize why mmm wasn't working)

SimpleDateFormat sDateFormat = new SimpleDateFormat("DD-MMM-yyyy");

sqlEnd = new Date(sDateFormat.parse(dateToday).getTime());

Hope this helps someone in the future, and thanks again paul.

Aknibbsa at 2007-7-8 1:36:40 > top of Java-index,Java Essentials,Java Programming...
# 3
http://www.exampledepot.com/egs/java.text/FormatDate.html
kilyasa at 2007-7-8 1:36:40 > top of Java-index,Java Essentials,Java Programming...
# 4

Perhaps I jumped the gun a bit on my thinking it works perfectly. It works for both the year and the day, but not for the month.

I am using the following code (This was the smallest base test i could think of):

SimpleDateFormat sDateFormat = new SimpleDateFormat("DD-MMM-yyyy", Locale.ENGLISH);

logger.warn("BADConverter : " + sDateFormat.parse("05-MAR-2005") );

And the result that I am getting is :

BADConverter : Wed Jan 05 00:00:00 CST 2005

Is there something obvious that I am missing here/ a footnote that I am missing ?

thanks again

*Edit*

Removed the side question about the api as it now makes sense.

Aknibbsa at 2007-7-8 1:36:40 > top of Java-index,Java Essentials,Java Programming...
# 5
Reread the API. Case is important. :-P
es5f2000a at 2007-7-8 1:36:41 > top of Java-index,Java Essentials,Java Programming...
# 6
> Reread the API. Case is important. :-PApox on the infurnal taller letters. Thanks for the polite "Schmuk you're doing it wrong" ;-P
Aknibbsa at 2007-7-8 1:36:41 > top of Java-index,Java Essentials,Java Programming...