如果您正在寻找 按日期范围过滤 的 常规用法 (例如,从今天到明天)。我用这种方式:
YourService.java
Date fromTimestamp = new Date();Date toTimestamp = new Date();Date fromDate = DateHelper.getDateWithoutTime(fromTimestamp);Date toDate = DateHelper.getDateWithoutTime(DateHelper.getTomorrowDate(toTimestamp));
YourDAO.java
criteria.add(Restrictions.ge(TIMESTAMP_FIELD, fromDate));criteria.add(Restrictions.le(TIMESTAMP_FIELD, toDate));
DateHelper.java
public static Date getDateWithoutTime(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime();}public static Date getTomorrowDate(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, 1); return cal.getTime();}当然,每个代码中始终都有重构的空间。



