File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem - how to ask for date of birth Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem - how to ask for date of birth" Watch "Problem - how to ask for date of birth" New topic
Author

Problem - how to ask for date of birth

Emma Lucy
Greenhorn

Joined: Apr 09, 2004
Posts: 12
Hi,
I have a predefined corejava.Day class that formats the date [yyyy,mm,dd]. I need to ask for date of birth, but just can't figure it out - bit of a newbie sorry! Code below from patient class, sets the date of birth at today's date, which is obviously incorrect but like to show I'm trying!
Code in patient class:

All the patient info is requested from the user called by method in the interface class. I realise that I need to ask for the date of birth here, but my program has to compile & I can't make it work if included.
Code in interface class:

All suggestions very gratefully received.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See the JavaDoc for DateFormat. You can use this to parse a String into a Date object. So you could ask the user to type a string in the right format, say YYYY/MM/DD or MM/DD/YY or whatever you tell DateFormat to use, and then parse that into a Date object that you store in Patient. Yikes, lousy sentence structure. Did it make sense?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Emma Lucy
Greenhorn

Joined: Apr 09, 2004
Posts: 12
Hi,
Thanks for your reply Stan - the sentence did make sense (I think!) but still not sure how to apply it to my code. Any more suggestions please?
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Hi Emma,
In the interface code you need another input like:

Then edit your Patient class, adding another argument to the constructor to accept this third DOB String and make your dateOfBirth instance variable of type String instead of type Day:

This will get it working. However, you will need to look into Stan's comment about using a Date type or DateFormat type as your dateOfBirth variable if you later want to get into working with the date entry as a real date (e.g. to compare it to another date or work out someones age).
Hope this helps a bit anyways


SCJP 1.4, www.gsi3d.org.uk
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Using DateFormat to parse string into a date gives you two things. First, if the input is not in valid format it will throw an exception so you'll know about it. That's a lot easier than trying to test if the number of days is valid for the month except in leap years and so on. Second, you get a Date object out of the deal. That's a lot easier than a String when you have to compare to other dates or do arithmetic like add 7 days or compute age from birth date.
I don't use these classes often enough to remember details, so this isn't real code, but the idea is something like:

Think about a method Date readDate(String prompt) that displays the prompt to the user and reads lines until it gets a good date. You could call it for birthdate, anniversary date, any date.
Having fun yet?
Kamil Khan
Greenhorn

Joined: Apr 25, 2003
Posts: 2
Hi Emma,
Try this
Date date = (new SimpleDateFormat("dd-M-yyyy")).parse(
Console.readString("Date of birth(MM/DD/YYYY)? "));
Khan
Emma Lucy
Greenhorn

Joined: Apr 09, 2004
Posts: 12
Hi Guys,
Thanks for all your replies, sorry for the response delay, but my head has been stuck firmly in the code & has only just emerged!!!
I've solved it by asking for a string format dd/mm/yy. Works a treat!
Thanks again,
EL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem - how to ask for date of birth