age compute using calendar class [problem]

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

Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

age compute using calendar class [problem]

 
0
  #1
Sep 4th, 2009
i just can't figure out what's wrong with this.

1. suppose i enter 9-3-1991 as the birthdate, this should return 18. but it returns 17. //wrong
2. suppose i enter 2-9-1991 as the birthdate, this returns 18. //correct

what's wrong with my code?
  1. private static int ageCalc(int mm, int dd, int yyyy) {
  2.  
  3. Calendar currentDate = Calendar.getInstance();
  4. Calendar birthDate = new GregorianCalendar();
  5.  
  6. birthDate.set(yyyy, mm, dd);
  7.  
  8. age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
  9.  
  10. birthDate.add(Calendar.YEAR, age);
  11.  
  12. if(currentDate.before(birthDate)) {
  13. age--;
  14. }
  15.  
  16. return age;
  17. }
a joke.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: age compute using calendar class [problem]

 
1
  #2
Sep 5th, 2009
Originally Posted by scias23 View Post
i just can't figure out what's wrong with this.

1. suppose i enter 9-3-1991 as the birthdate, this should return 18. but it returns 17. //wrong
  1. private static int ageCalc(int mm, int dd, int yyyy){
  2. // code
  3. birthDate.set(yyyy, mm, dd);


I'd have to see the function call, but if you are doing this:

  1. ageCalc (9, 3, 1991)

and passing 9 to the function as mm, realize that 9 is October, not September. January to December are 0 to 11, not 1 to 12.

http://java.sun.com/javase/6/docs/ap...ndar.SEPTEMBER

If you scroll to SEPTEMBER, you'll see that it is equal to 8.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: age compute using calendar class [problem]

 
0
  #3
Sep 5th, 2009
hey thanks! it works!
a joke.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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