This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
from java i want to pass the date in "yyyy-mm-dd" format to a stored procedure. The variable which i use to pass is of type string which will sent the date in "dd-mm-yyyy" format. My question is how to convert a string which is in "dd-mm-yyyy" format to "yyyy-mm-dd" format.
I haven't compiled this, so there may be off-by-one errors, but you get the gist. The various parts can have different lengths, which might otherwise be a common source of errors. If you can be sure that days and months are always 2 digits, and the year 4 digits, then there is no need for the dashOne and dashTwo variables.
The proper way to do this is to use the java.text.SimpleDateFormat class, of course, but I'm sure someone else will post a snippet of code that shows that. [ June 29, 2006: Message edited by: Ulf Dittmer ]
If you have to do millions of these you might time the two approaches. I found string manipulation to be about 100x faster than parsing one string into a Date and then formatting the date into another string. If you try the timings see how many iterations you'd have to do before the time difference is something the user would notice.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi