Java Gregorian Date Problem

Have a problem calculating the "Age Next Birthday", given the current date and a date of birth.

The problem seems to be apparent on AIX, but not on my local Windows box, both running WebSphere 4.

The problem is this: When I enter a DAY of 01 or 02 of the month for the date of birth, then the age next birthday is a year greater than it should be.

Here is the function I use for the calculation

public static int getAge(Date dateOfBirth, boolean anbFlag)
{


     Calendar dob = GregorianCalendar.getInstance();
     dob.setTime(dateOfBirth);
     Calendar present = GregorianCalendar.getInstance();
               
      int age;
     age = present.get(GregorianCalendar.YEAR) - dob.get(GregorianCalendar.YEAR);
        if (age > 0)
        {
         if (dob.get(GregorianCalendar.MONTH) > present.get(GregorianCalendar.MONTH))
         {
               age=age-1;
         }
            else if((dob.get(GregorianCalendar.MONTH) == present.get(GregorianCalendar.MONTH))
        && (dob.get(GregorianCalendar.DAY_OF_MONTH) > present.get(GregorianCalendar.DAY_OF_MONTH)))
           {
                age=age-1;
           }
        }
       
        if(anbFlag)
             age++;
                                       
     return age;
     
}

lvealeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 Take a look at this statement:

 if (dob.get(GregorianCalendar.MONTH) > present.get(GregorianCalendar.MONTH))
        {
              age=age-1;
        }
           else if((dob.get(GregorianCalendar.MONTH) == present.get(GregorianCalendar.MONTH))
       && (dob.get(GregorianCalendar.DAY_OF_MONTH) > present.get(GregorianCalendar.DAY_OF_MONTH)))
          {
               age=age-1;
          }
       }

  you subtract one of the age regardless of the condition (in both cases you subtract one), but you do not have any satement that checks what happens if the dob month is less than the current month.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
Add some debug to print out the dateOfBirth and the calculated day/month/year values.

> When I enter a DAY of 01 or 02 of the month for the date of birth

How do you construct your date of birth variable?

0
Bob_EverardCommented:
Here's a simple one:

import java.util.*;
public class dob {
   public static void main( String[] args ) {
      int by = Integer.parseInt( args[ 0 ] );
      int bm = Integer.parseInt( args[ 1 ] );
      int bd = Integer.parseInt( args[ 2 ] );
      Calendar dob = new GregorianCalendar( by, bm - 1, bd );
      Calendar now = new GregorianCalendar();
      int age = now.get( Calendar.YEAR ) - dob.get( Calendar.YEAR );
      if ( age > 0 ) {
         int md = now.get( Calendar.MONTH ) - dob.get( Calendar.MONTH );
         int dd = now.get( Calendar.DAY_OF_MONTH ) -
                  dob.get( Calendar.DAY_OF_MONTH );
         age -= md < 0 ? 1: md > 0 ? 0: dd < 0 ? 1: 0;
      } else {
         System.out.println( "You are lying about your age" );
         System.exit( 4 );
      } // if
      System.out.println( "Your age is now " + age );
   }
}
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

CleanupPingCommented:
lveale:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
TimYatesCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept girionis' comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
0
girionisCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.