0

I am trying to figure out how to get a date that is exactly one year less than the current date and pass it as a parameter to HQL.

I tried to use Calendar. But I am lost in converting this to right Date format required for Oracle.

Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.YEAR, -1); String dateLimit = cal.getTime().toString(); Date dateFormatter = new SimpleDateFormat("dd-MMM-yyyy").parse(dateLimit); // This one shows I need to either wrap it up in try catch or specify in throws. //Date cutOffDate = dateFormatter.parse(dateLimit); //tried this. no avail queryBuilder.append(" and c.dateOfContact >= :cutOffDate "); parameters.put("cutOffDate", dateFormatter); 

Alternatively, are there any Hibernate built-in function for date manipulation? All I want is to pass a date to the query, which is a year less than the current date.

Oracle date format is dd-MMM-yyyy. (ex: 21-Jun-2013)

2 Answers 2

1

i think this will work:

parameters.put("cutOffDate", cal.getTime()); 

If the type of c.dateOfContact is java.util.Date or java.sql.Date than cal.getTime() has to work.

Sign up to request clarification or add additional context in comments.

Comments

1
Date d = cal.getTime(); Query query = em.createQuery(QUERYSTRING)setParameter("cutOffDate", d); 

The above should work, don't format your date as a string, because then you will be comparing a String and a Date.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.