aspose file tools*
The moose likes JDBC and the fly likes Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?" Watch "Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?" New topic
Author

Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?

Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

Hi Guys,

I'm developing an application where the user selects the date in (MM/dd/yyyy) format.
Now, i convert the String (date recieved from user) to (yyyy-mm-dd) format by using the following java code ,
send it to valueOf() method of java.sql.Date class and then storing into DB.

public String convertToSqlDate(String date){
String converted_date="";

converted_date+=date.substring(date.lastIndexOf('/')+1,date.length());
converted_date+="-"+date.substring(0,date.indexOf('/'));
converted_date+="-"+date.substring(date.indexOf('/')+1,date.lastIndexOf('/'));

return converted_date;

}

Is there any better way to implement the same?

Thanks in advance


Thanks & Regards
Gopakumar
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Yes, there is - by using date fromats:

Anyways, this is not right approach to handle dates in context of database.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Zandis Murāns wrote:Anyways, this is not right approach to handle dates in context of database.

Zandis, please expand on what is wrong with that (SimpleDateFormat) approach.

The only thing else I can think of is to use a LOCALE to determine if the user prefers DD/MM/YYYY (or some other variation). Is that what you're thinking about?

Joe
 
wood burning stoves
 
subject: Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?