age compute using calendar class [problem]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: age compute using calendar class [problem]

  1. #1
    Join Date
    Dec 2008
    Posts
    19

    age compute using calendar class [problem]

    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?
    Code:
        private static int ageCalc(int mm, int dd, int yyyy) {
     
            Calendar currentDate = Calendar.getInstance();
            Calendar birthDate = new GregorianCalendar();
     
            birthDate.set(yyyy, mm, dd);
     
            age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
     
            birthDate.add(Calendar.YEAR, age);
     
            if(currentDate.before(birthDate)) {
                age--;
            }
     
            return age;
        }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    take a look at your before method. That appears to be the source of the error: it the date is on or before the current date then the method should return a false or don't reduce age message. Or if before OR same ...

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 11:09 AM
  3. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  4. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center