• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 823
  • Last Modified:

How to convert a user entered date (dd/mm/yy) into a format for a mysql database (yyyy/mm/dd)?

An HTML form requires the user to enter a date. As this is uk, I would like the user to be able to enter in a format - dd/mm/yy. I am not familiar enough with the java Date classes to be able to convert this into a format suitable to be entered into a mysql database. Do I need to parse the String and create a Date object first before using SimpleDateFormat?  Would appreciate some assistance. Thank you.
0
SimonAllen
Asked:
SimonAllen
2 Solutions
 
girionisCommented:
Use the SimpleDateFormat:

SimpleDateFormat sdf = new SimpelDateFormat("YYYY/MM/dd");
Date d = sdf.parse(<date from form>);
0
 
girionisCommented:
Typo:

> SimpleDateFormat sdf = new SimpelDateFormat("YYYY/MM/dd");

should be

SimpleDateFormat sdf = new SimpelDateFormat("yyyy/MM/dd");
0
 
girionisCommented:
> Do I need to parse the String and create a Date object first before using SimpleDateFormat?

No, just get the String from the HTML form and parse it liek I did in my example. Then you can create a date object to insert into the database with the new format (yyyy/mm/dd)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mukundha_expertCommented:
SimpleDateFormat sdf = new SimpelDateFormat("dd/mm/yy");
java.util.Date d = sdf.parse(<date from form>);

to insert this date into your database,

java.sql.Date date = new java.sql.Date ( d.getTime () ) ;

0
 
mukundha_expertCommented:
actually it should be ,

SimpleDateFormat sdf = new SimpelDateFormat("dd/MM/yy");
0
 
girionisCommented:
> SimpelDateFormat("dd/mm/yy");

Bah... you are right, this is the format in the form... I thought the other way round.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
girionisCommented:
> As this is uk, I would like the user to be able to enter in a format - dd/mm/yy

If you are talking about entering the date in the proper format in the form, then you should use JavaScript to do it: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21449630.html
0
 
CEHJCommented:
You would be better off not allowing the user to type anything at all, or you may get errors
0
 
SimonAllenAuthor Commented:
Thank you guys - your solution works of course. By way of explanation (for CEHJ) the application is designed to allow pilots to log their flying hours so the date entered by the user will not necessarily be the current date. Also, it did occur to me that I do not have to use a Date object at all. I could transform the string from the form(dd/mm/yy) to another string(yyyy/mm/dd) using java's substring method. This could be entered into the mysql database as is. Not quite sure which method is best yet. Many thanks anyway, Simon
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now