Solved

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

Posted on 2006-07-12
10
804 Views
Last Modified: 2012-08-13
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
Comment
Question by:SimonAllen
10 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17089554
Use the SimpleDateFormat:

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

Expert Comment

by:girionis
ID: 17089558
Typo:

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

should be

SimpleDateFormat sdf = new SimpelDateFormat("yyyy/MM/dd");
0
 
LVL 35

Expert Comment

by:girionis
ID: 17089562
> 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17089565
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
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 45 total points
ID: 17089576
actually it should be ,

SimpleDateFormat sdf = new SimpelDateFormat("dd/MM/yy");
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 80 total points
ID: 17089587
> SimpelDateFormat("dd/mm/yy");

Bah... you are right, this is the format in the form... I thought the other way round.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17089669
0
 
LVL 35

Expert Comment

by:girionis
ID: 17089679
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17090338
You would be better off not allowing the user to type anything at all, or you may get errors
0
 

Author Comment

by:SimonAllen
ID: 17091801
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question