?
Solved

Java Gregorian Date Problem

Posted on 2003-03-03
7
Medium Priority
?
304 Views
Last Modified: 2007-12-19
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;
     
}

0
Comment
Question by:lveale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 800 total points
ID: 8056537
 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
 
LVL 92

Expert Comment

by:objects
ID: 8060900
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
 
LVL 2

Expert Comment

by:Bob_Everard
ID: 8226928
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 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Expert Comment

by:CleanupPing
ID: 9058965
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9721251
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
 
LVL 35

Expert Comment

by:girionis
ID: 9721260
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question