I am using a SQL Server database and Hibernate. I have the following problem related to storing dates:
I have the following code:
String hql = "select user from User user where user.createDate = :date";
Query query = HibernateUtil.getSession().createQuery(hql);
Date date2 = DateHelper.getEndOfDay(new Date());//this returns 2010-07-27 23:59:59.999
query.setParameter("date", date2);
When I am comparing the date 2010-07-27 23:59:59.999
I am getting results that have a date: 2010-07-28 00:00:00.0
. Using the setDate() instead of setParameter() returns the users whose saved dates are on the same day, meaning that the hours, minutes etc. are discarded.
The hibernate maps the dates to java.util.Date but in the result list everything is Timestamp I guess because Timestamp extends java.util.Date. What would you suggest to do in order to have a good result when comparing for equality?