[英]Hibernate Query to fetch records on date ignoring the year
I want to fetch all the records which match the today date but ignoring the year Date in database is like this 1980-11-14
. 我想获取与今天的日期匹配的所有记录,但是忽略数据库中的年份Date就像这样的1980-11-14
。 Want to fetch the record only comparing month and day not the year. 想要只比较月份和日期而不是年份来获取记录。 I tried it using Criteria
but its not working. 我使用Criteria
尝试,但无法正常工作。 Here is my code. 这是我的代码。
Date date = new Date(); // your date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Calendar fromDate = calendar.getInstance();
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Calendar toDate = calendar.getInstance();
Criteria criteria=session.createCriteria(Add_Follower.class);
criteria.add(Restrictions.eq("follow_uInfo.id", uid));
criteria.add(Restrictions.between("following_uInfo.dob",fromDate,toDate));
users=criteria.list();
I did it with a named query and using "EXTRACT", 我使用命名查询并使用“ EXTRACT”完成了此操作,
in this example for birthdays: 在此示例中,生日:
SELECT p FROM Person p
WHERE EXTRACT(DAY FROM p.gebdat) = EXTRACT(DAY FROM NOW())
AND EXTRACT(MONTH FROM p.gebdat) = EXTRACT(MONTH FROM NOW())
ORDER BY p.nachname, p.vorname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.