| Author |
Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?
|
Gopakumar Naryanan
Ranch Hand
Joined: Jan 15, 2011
Posts: 71
|
|
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: 181
|
|
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
|
 |
 |
|
|
subject: Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?
|
|
|