• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gopakumar Naryanan
Ranch Hand
Posts: 72
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is - by using date fromats:

Anyways, this is not right approach to handle dates in context of database.
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic