2014-04-15 91 views
0

我有這個代碼,我正在使用從DOB計算年齡。但我不確定它是否正常工作,或者當我從另一個文件調用此函數時,我不是能夠看到任何輸出。計算從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> 

誰能幫我在指着什麼可能是這個原因?

UPDATE1:的SQL查詢工作correctly.I執行它在MySQL Workbench和它在這種格式YYYY-MM-DD 00:00:00

UPDATE2返回結果:我使用的這個結果生成一個圖表其詳細信息在Generating Histogram through Java and PrimeFaces

+2

使用JodaTime,將數據庫中的值轉換爲DateTime對象 – MadProgrammer

+0

我不確定你的意思聲明,你「無法看到任何輸出「。您的代碼中是否存在一些記錄或System.out語句? –

+0

@MadProgrammer問題是我在使用API​​時受到了限制。系統不支持它。你可以看看代碼,並在出現任何錯誤時指出幫助嗎? – user2916886

回答

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 

現在,這個問題,你會得到各種奇怪的四捨五入錯誤,所以它是,至多,猜測......