trying to do a date of birth comparison.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 2
Reputation: Fredjava is an unknown quantity at this point 
Solved Threads: 0
Fredjava Fredjava is offline Offline
Newbie Poster

trying to do a date of birth comparison.

 
0
  #1
Mar 14th, 2006
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.

  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();

Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 259
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: trying to do a date of birth comparison.

 
0
  #2
Mar 14th, 2006
hi,

here is a simple program that gives you your age month and day...give the date of birth as an argument though...
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC