- 參考資料
這樣只會查詢到時間戳年月日為 20230523 的 row
select * from some.table where starttime between '2023-05-23' and '2023-05-24'; |
用 JPA 改寫
日期比較適合下區間,除非是很確定的時間
# 補充資料
- Java 取得今天日期 1
Java 取得今天日期 2- 後來沒採用
- 日期加一天
- 雖然下面沒試到,但此文有個冷知識 sql.Date 繼承自 util.Date
# 最後的實踐
REPO
List<XxxEntity> findAllByTimeBetween(LocalDateTime d1, LocalDateTime d2); |
DAO
public List<XxxEntity> findAllByTimeBetween(LocalDateTime d1, LocalDateTime d2) { | |
return repository.findAllByTimeBetween(d1,d2); | |
} |
Service 中的查詢過程
LocalDateTime d1 = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); // 這是今天 | |
List<XxxEntity> xxxEntities= xxxDao.findAllByTimeBetween(d1,d1.plusDays(1)); // 傳入今天跟 + 1 天 |
用 LocalDate 型態遇到的錯誤:這是因為 ORM 版本比較舊,不認識 LocalDate QQ,所以才如上方改用 LocalDateTime (而且加一天/減一天的 method 已經有提供比較方便)
