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

    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?
        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)) {
            return age;

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    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: 27
    Last Post: 02-11-2016, 05:29 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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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