2017-10-04 68 views
1

後 - 春季啓動。我想創建一個JPA查找來計算兩個日期之間的行數。春數據JPA - 構建查詢我是新來的Java JPA之前和日期

我需要得到一個數字顯示,在過去28天內加入成員的人數。所以類似10

而且還得到一個值,該值指示從上個月的區別 - 所以+ 4% - 或-2% - 所以我想這將是一個COUNT1/COUNT2 * 100 =差異。你如何檢測極性 - 評估它的否定或正面?

目前,我有這樣的事情

long countByRegisteredDateAfter(Date thresholdDate) throws Exception; 

但需要的東西也許更喜歡關注像這樣

long countByRegisteredBeforeAndDateAfterAndRole(Date thresholdDate1, Date thresholdDate2, String role) 
    throws Exception; 

代碼到目前爲止,這

long countByRegisteredBeforeDateAfter(Date thresholdDate1, Date thresholdDate2) 
    throws Exception; 

,也許更多的東西罰款:

  // 28 days ago 
      Calendar thresholdPast28 = Calendar.getInstance(); 
      thresholdPast28.set(Calendar.HOUR_OF_DAY,0); 
      thresholdPast28.set(Calendar.MINUTE,0); 
      thresholdPast28.set(Calendar.SECOND,0); 
      thresholdPast28.add(Calendar.DATE,-28); 

      java.util.Date thresholdPast28Date = thresholdPast28.getTime(); 
      Long countLast28Days = (Long) tblLoginRepository.countByRegisteredDateAfter(thresholdPast28Date); 

      System.out.println("countLast28Days " + countLast28Days); 


      // 56 days ago 
      Calendar thresholdPast56 = Calendar.getInstance(); 
      thresholdPast56.set(Calendar.HOUR_OF_DAY,0); 
      thresholdPast56.set(Calendar.MINUTE,0); 
      thresholdPast56.set(Calendar.SECOND,0); 
      thresholdPast56.add(Calendar.DATE,-28); 

      java.util.Date thresholdPast56Date = thresholdPast56.getTime(); 
      Long countLast56Days = (Long) tblLoginRepository.countByRegisteredDateAfter(thresholdPast56Date); 

      System.out.println("countLast56Days " + countLast56Days); 

回答

4

我有點困惑,因爲你想嘗試在以後你想擁有它們之間前後一段時間後,發現日期話題狀態。然而得到的嘗試之間的日期:

long countByRegisteredDateBetween(Date thresholdDate1, Date thresholdDate2) 

或在第二個例子中

long countByRegisteredDateBetweenAndRole(Date thresholdDate1, Date thresholdDate2, String role) 

,並得到前和嘗試像後:

long countByRegisteredDateBeforeAndRegisteredDateAfter(Date thresholdDate1, Date thresholdDate2) 

類似做與角色的情況下

+0

謝謝@pezetem - 也來到了這個解決方案。 –