943,692 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 665
  • Java RSS
Sep 4th, 2009
0

age compute using calendar class [problem]

Expand 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
2. suppose i enter 2-9-1991 as the birthdate, this returns 18. //correct

what's wrong with my code?
java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Sep 5th, 2009
1

Re: age compute using calendar class [problem]

Click to Expand / Collapse  Quote originally posted by scias23 ...
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
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 5th, 2009
0

Re: age compute using calendar class [problem]

hey thanks! it works!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Client and server with objects
Next Thread in Java Forum Timeline: index on record





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


Follow us on Twitter


© 2011 DaniWeb® LLC