• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 72
Hibernate Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic