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:
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
Joined: Apr 09, 2004
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?
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
Joined: Jan 29, 2003
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?
Hi Emma, Try this Date date = (new SimpleDateFormat("dd-M-yyyy")).parse( Console.readString("Date of birth(MM/DD/YYYY)? ")); Khan
Joined: Apr 09, 2004
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com