Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversion of date

 
zaheer agadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic