Abhijeet Jadhav
Abhijeet Jadhav

Reputation: 25

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. Want to fetch the record only comparing month and day not the year. I tried it using Criteria but its not working. 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();

Upvotes: 1

Views: 1189

Answers (1)

Christian
Christian

Reputation: 11

I did it with a named query and using "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

Upvotes: 1

Related Questions