user2916886
user2916886

Reputation: 845

Calculating age from DOB extracted from database

I have this code which I am using to calculate age from DOB.But I am not sure whether it is working properly or not as when I am calling this function from another file, I am not able to see any output.

public ArrayList<Integer> getEmployeeAge() {
        // TODO Auto-generated method stub


        ArrayList<Integer> employeeage = new ArrayList<Integer>();
        ResultSet rs = null;

        try {
            LibraryConnection lc = new LibraryConnection(library, dbConnection);


            rs = lc.executeQuery("GetEmployeeAge", null);


            while(rs.next()){   
/*              QuartReport qcd = new QuartReport();

                qcd.setYear(rs.getInt(1));
                qcd.setQuarter(rs.getInt(2));
                qcd.setCount(rs.getInt(3));
*/

                String dob = rs.getString(1);

                int yearDOB = Integer.parseInt(dob.substring(6, 10));
                int monthDOB = Integer.parseInt(dob.substring(3, 5));
                int dayDOB = Integer.parseInt(dob.substring(0, 2));

                //CALCULATE THE CURRENT YEAR, MONTH AND DAY
                //INTO SEPERATE VARIABLES
                DateFormat dateFormat = new SimpleDateFormat("yyyy");
                java.util.Date date = new java.util.Date();
                int thisYear = Integer.parseInt(dateFormat.format(date));

                dateFormat = new SimpleDateFormat("MM");
                date = new java.util.Date();
                int thisMonth = Integer.parseInt(dateFormat.format(date));

                dateFormat = new SimpleDateFormat("dd");
                date = new java.util.Date();
                int thisDay = Integer.parseInt(dateFormat.format(date));

                //CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
                //TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
                //OF THE DOB
                int age = thisYear - yearDOB;
                //IF THE CURRENT MONTH IS LESS THAN THE DOB MONTH
                //THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
                //BIRTHDAY YET THIS YEAR
                if(thisMonth < monthDOB){
                age = age -1;
                }

                //IF THE MONTH IN THE DOB IS EQUEL TO THE CURRENT MONTH
                //THEN CHECK THE DAY TO FIND OUT IF THEY HAVE HAD THEIR
                //BIRTHDAY YET. IF THE CURRENT DAY IS LESS THAN THE DAY OF THE DOB
                //THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
                //BIRTHDAY YET THIS YEAR
                if(thisMonth == monthDOB && thisDay < dayDOB){
                age = age -1;
                }

                employeeage.add(age);
            }

            return employeeage;
        }catch (SQLException e) {
            e.printStackTrace();
        }catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        }finally {
            SqlUtils.close(rs);
        }


        return null;
    }

The xml query that I am executing is:

<s:query name="GetEmployeeAge" xmlns:s="http://www.slamb.org/axamol/sql-library/statement">
<s:sql databases="mysql">
SELECT DOB(`Date Of Birth`) 
FROM `Employee` LEFT JOIN Employee-Detail ON `Employee.MRN` = `Employee-Detail.MRN`
GROUP BY DOB(`Date Of Birth`)
ORDER BY DOB(`Date Of Birth`) ASC
</s:sql>
</s:query>

Can anyone help me in pointing what might be the reason for this?

UPDATE1: The SQL query is working correctly.I executed it on MySQL workbench and it is returning the result in this format YYYY-MM-DD 00:00:00

UPDATE2: I am using the result of this to generate a graph whose details are present at Generating Histogram through Java and PrimeFaces

Upvotes: 0

Views: 1403

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

The biggest problem you have is the fact that a year and a month are not a whole number, this is why we suggest using a library which is been properly designed to handle these weirdnesses...like Joda-Time...

A year has 365.242 days which makes a month 30.436 (approximately) days long...:P

public class DateOfBirth {

    public static final double DAYS_IN_YEAR = 365.242;
    public static final double DAYS_IN_MONTH = DAYS_IN_YEAR / 12d;

    public static void main(String[] args) {
        int day = 8;
        int month = 3;
        int year = 1972;

        // Number of days...
        double time = toDays(year, month, day);
        // One day less of the year 2015...
        double today = toDays(2014, 4, 16); // today...

        double diff = today - time;

        int years = (int)Math.round(diff / DAYS_IN_YEAR);
        double over = diff % DAYS_IN_YEAR;
        int months = (int)(over / DAYS_IN_MONTH);
        over = over % DAYS_IN_MONTH;
        int days = (int) over;

        System.out.println(years + " years, " + months + " months, " + days + " days");           
        System.out.println(NumberFormat.getNumberInstance().format(diff / DAYS_IN_YEAR));            
    }

    public static double toDays(int year, int month, int day) {            
        return (year * DAYS_IN_YEAR) + (month * DAYS_IN_MONTH) + day;            
    }

}

This outputs...

42 years, 1 months, 7 days
42.105

Now, the problem with this, is you will get all sorts of strange rounding errors, so it is, at best, a guess...

Upvotes: 1

Related Questions