2015-05-04 69 views
-1

我想從數據庫中獲取特定日期發生的結果。問題是數據庫包含日期+時間,但我只需要基於日期部分的結果。根據db的日期提取結果,而不考慮時間

@Query("from TableA where traveller = ?1 and direction = ?2 and targetDate = ?3") 
List<TableA> findListOfRequestsWithDateAndTraveller(String traveller, String direction, Date date); 
+0

您正在使用哪個數據庫,對於Oracle,您可以使用postgres中繼時間並過濾日期 –

+0

。 –

+0

@SaurabhJhunjhunwala這是殺死日期時間列索引的最佳方法。應該做的是在午夜的某一天(包括)和下午的第二天(不包括)作爲上限。 – fge

回答

0

我被下的Java,而不是查詢處理它解決了這個問題。

@Query("from TableA where traveller = ?1 and direction = ?2 and targetTime >= ?3 and targetTime <= ?4") 
List<PBillableRequest> findListOfRequestsWithDateAndTraveller(String traveller, String direction, Date startDate, Date endDate); 

獲取startDate和endDate,我使用了這個函數。

public Date getEndOfDay(Date date) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    calendar.set(Calendar.HOUR_OF_DAY, 23); 
    calendar.set(Calendar.MINUTE, 59); 
    calendar.set(Calendar.SECOND, 59); 
    calendar.set(Calendar.MILLISECOND, 999); 
    return calendar.getTime(); 
} 

public Date getStartOfDay(Date date) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.MILLISECOND, 0); 
    return calendar.getTime(); 
} 
0

嘗試使用日期功能,在這裏找到:date(timestamp_field)

@Query("from TableA where traveller = ?1 and direction = ?2 and date(targetDate) = ?3") 
List<TableA> findListOfRequestsWithDateAndTraveller(String traveller, String direction, Date date); 
相關問題