• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem - how to ask for date of birth

 
Emma Lucy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Emma Lucy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emma,
Try this
Date date = (new SimpleDateFormat("dd-M-yyyy")).parse(
Console.readString("Date of birth(MM/DD/YYYY)? "));
Khan
 
Emma Lucy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic