我有这个代码用于从DOB计算年龄。但是我不确定它是否正常工作,因为当我从另一个文件调用此函数时,我无法看到任何输出。 / p>
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>
任何人都可以帮我指出可能是什么原因吗?
UPDATE1: SQL查询工作正常。我在MySQL工作台上执行了它,并以这种格式返回结果YYYY-MM-DD 00:00:00
UPDATE2 :我使用此结果生成一个图表,其详细信息显示在Generating Histogram through Java and PrimeFaces
答案 0 :(得分:1)
你遇到的最大问题是,一年零一个月不是一个整数,这就是为什么我们建议使用一个经过适当设计的库来处理这些奇怪的东西......比如Joda-Time。 ..
一年有365.242天,一个月30.436(约)天......: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;
}
}
这输出......
42 years, 1 months, 7 days
42.105
现在,问题是,你会得到各种奇怪的舍入错误,所以它充其量只是猜测......