Java 从数据库中提取DOB计算年龄

Java 从数据库中提取DOB计算年龄,java,sql,Java,Sql,我有这个代码,我用它来计算DOB的年龄。但我不确定它是否正常工作,因为当我从另一个文件调用这个函数时,我无法看到任何输出 public ArrayList<Integer> getEmployeeAge() { // TODO Auto-generated method stub ArrayList<Integer> employeeage = new ArrayList<Integer>(); Result

我有这个代码,我用它来计算DOB的年龄。但我不确定它是否正常工作,因为当我从另一个文件调用这个函数时,我无法看到任何输出

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;
    }
我正在执行的xml查询是:

<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>
有人能帮我指出这可能是什么原因吗

更新1:SQL查询工作正常。我在MySQL工作台上执行了它,它以YYYY-MM-DD 00:00:00格式返回结果


更新2:我正在使用这一结果生成一个图表,其详细信息显示在

中。您遇到的最大问题是,一年和一个月不是一个整数,这就是为什么我们建议使用一个经过适当设计的库来处理这些奇怪的情况…例如

一年有365.242天,这使得一个月大约有30.436天

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;            
    }

}
这将输出

42 years, 1 months, 7 days
42.105

现在,问题是,你会得到各种奇怪的舍入错误,因此,充其量只是猜测…

使用JodaTime,将数据库中的值转换为DateTime对象我不确定你所说的不能看到任何输出的语句是什么意思。您的代码中是否缺少一些日志记录或System.out语句?@MadProgrammer问题是我在使用API时有限制。系统不支持它。您能看看代码并指出是否有任何错误来帮助我吗?@Mattforsyth我正在调用此函数,然后使用它生成图形,而我没有让任何输出可见,而不是通过错误或异常分离关注点,这将对您有很大帮助:如果该方法没有从数据库中提取DOB并计算相关年龄,那么您可以简单地对给定DOB计算年龄的方法进行单元测试。