Home:ALL Converter>Correct way to compare dates with Hibernate and Oracle, taking account of daylight savings

Correct way to compare dates with Hibernate and Oracle, taking account of daylight savings

Ask Time:2018-10-30T20:47:00         Author:user155631

Json Formatter

I have some Java Hibernate code which is intended to select those rows in an Oracle table where a DATE value (mapped to startDate) is at least one hour ago. To test this, I insert a row directly with a value of Oracle's sysdate and then run the code. Prior to the switch back from BST at the weekend, my code was returning the row, but now zero rows are being returned. I would have expected this to always return zero rows. Is my code correct?

int cutOffMillis = 1 * 3600 * 1000;
Calendar cal = GregorianCalendar.getInstance();
cal.add(Calendar.MILLISECOND, -cutOffMillis);
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(DbReport.class);
criteria.add(Restrictions.lt("startDate", cal.getTime()));
return criteria.list();

Author:user155631,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53064645/correct-way-to-compare-dates-with-hibernate-and-oracle-taking-account-of-daylig
yy