Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

conversion of date

 
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.
 
Rancher
Posts: 43016
76
  • 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 ]
 
(instanceof Sidekick)
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic