943,822 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8379
  • Java RSS
Mar 14th, 2006
0

trying to do a date of birth comparison.

Expand Post »
I am trying to write a date of birth compair. here is how i have started out but I am not getting the results I want to see I am wanting to enter the text as

03/14/73 and compaire that to todays date and tell the user how old they are in only years.

This is as far as I can get with it and I will be totaly truthfull I did not write the hole thing this is the beginging of a class assinment and I cant find were on the net to get help. Sugestions. Please.

Java Syntax (Toggle Plain Text)
  1. System.out.print("Enter the year you were born: ");
  2. usrYear = getYear(year);
  3. userYr = Integer.parseInt(usrYear);
  4.  
  5. System.out.print("Enter the month you were born: ");
  6. usrMonth = getMonth();
  7. userMth = Integer.parseInt(usrMonth);
  8.  
  9. System.out.print("Enter the day you were born: ");
  10. usrDay = getDay(userMth);
  11. userDay = Integer.parseInt(usrDay);
  12.  
  13. System.out.println("Your birthday is on " + userMth + "/" + userDay + "/" + userYr);
  14. displayAge(userYr, year, userMth, month, userDay, day);
  15. }
  16.  
  17. public static String getYear(int yr)throws Exception {
  18. int outYear=0;
  19. int year;
  20. year = yr;
  21. String usrYear = new String();
  22. String output = new String();
  23. usrYear = getString();

Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fredjava is offline Offline
2 posts
since Mar 2006
Mar 14th, 2006
0

Re: trying to do a date of birth comparison.

hi,

here is a simple program that gives you your age month and day...give the date of birth as an argument though...
Java Syntax (Toggle Plain Text)
  1. import java.util.Calendar;
  2. import java.util.Date;
  3. import java.util.Locale;
  4. import java.text.SimpleDateFormat;
  5. import java.text.ParseException;
  6.  
  7. public class Age {
  8.  
  9. public Age(long birthday) {
  10. Calendar age = Calendar.getInstance(Locale.getDefault());
  11. age.setTimeInMillis(Math.abs(birthday-System.currentTimeMillis()));
  12. System.out.println("Your are "+(age.get(Calendar.YEAR)-1970)+" years, "+age.get(Calendar.MONTH)+" month, "+age.get(Calendar.DAY_OF_MONTH)+" days "+(System.currentTimeMillis()-birthday<0 ?"Not Born Yet" :"Old")+".");
  13. }
  14.  
  15. public static void main(String[] args) {
  16. SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
  17. if (args.length==0) {
  18. System.out.println("Usage:\n java Age dd/mm/yyyy\nWhere:\n dd - date\n mm - month\n yyyy - year");
  19. } else {
  20. try {
  21. final Date birthday = dateFormatter.parse(args[0]);
  22. System.out.println("Your birthday is " + dateFormatter.format(birthday));
  23. final Age myAge = new Age(birthday.getTime());
  24. } catch (ParseException e) {
  25. System.out.println("Error parsing your birthday.");
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30.  
  31. }
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with input/output in classes
Next Thread in Java Forum Timeline: Pls Help JAVA Program Headaches Not Giving me the right output for user input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC