File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes conversion of date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "conversion of date " Watch "conversion of date " New topic

conversion of date

zaheer agadi
Ranch Hand

Joined: Jan 02, 2006
Posts: 32
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.

CHEERS<br />Zaheer SCJP(1.4)
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: conversion of date
jQuery in Action, 3rd edition