问题描述
SELECT x FROM SomeClass
WHERE x.dateAtt BETWEEN CURRENT_DATE AND (CURRENT_DATE + 1 MONTH)
在上述JPQL语句中,SomeClass有一个memebr dateAttr
,它是一个 java.util。日期
,并有一个 @Temporal(javax.persistence.TemporalType.DATE)
注释。
In the above JPQL statement, SomeClass has a memebr dateAttr
, which is a java.util.Date
and has a @Temporal(javax.persistence.TemporalType.DATE)
annotation.
我需要一种方式来执行(CURRENT_DATE + 1 MONTH)
位 - 在当前状态下显然是错误的 - 但是找不到JPQL的日期功能的文档
I need a way to do the (CURRENT_DATE + 1 MONTH)
bit - it is obviously wrong in its current state - but cannot find the doc with the date function for JPQL.
任何人都可以指向文档,文档JPQL日期功能(以及如何执行此特定查询)?
Can anyone point me in the direction of a doc that documents JPQL date functions (and also how to do this particular query)?
推荐答案
如果你有一个日期对象是+1个月,你可以这样做:
If you have a date object that is + 1 month already you could do something like this:
public List findEmployees(Date endDate) {
return entityManager.createQuery(
"SELECT e from Employee e WHERE e.startDate BETWEEN ?1 AND ?2")
.setParameter(1,new Date(), TemporalType.DATE)
.setParameter(2,endDate, TemporalType.DATE).getResultList();
}
然而,这要求日期在手之前有效。
This however, requires that the dates be valid before hand.
更新
如果您想要下个月,可以使用,它有一个伟大和容易的api。
然后您可以修改您的查询:
If you always want the next month, you can use JodaTime which has a great and easy api.You could then modify your query like this:
//Get next month
DateTime dt = new DateTime();
entityManager.createQuery(
"SELECT e from Employee e WHERE e.startDate BETWEEN ?1 AND ?2")
.setParameter(1,new Date(), TemporalType.DATE)
.setParameter(2,dt.plusMonths(1).toDate(), TemporalType.DATE).getResultList();
这篇关于Java:JPQL date函数将时间段添加到另一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!