如何比较休眠日期

时间:2012-04-09 13:26:09

标签: java spring hibernate

在我的数据基准日期为2012-04-09 04:02:53 2012-04-09 04:04:51 2012-04-08 04:04:51等,我需要检索在日期字段中具有当前日期的数据。我的意思是我只需匹配2012-04-09'。我怎么能用hibernate标准来做呢。

8 个答案:

答案 0 :(得分:29)

使用Restrictions.between()生成一个where子句,其日期列位于“2012-04-09 00:00:00”和“2012-04-09 23:59:59”之间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fromDate = df.parse("2012-04-09 00:00:00");
Date toDate = df.parse("2012-04-09 23:59:59");

criteria.add(Restrictions.between("dateField", fromDate, toDate));

请注意,Criteria API中使用的所有属性都是Java属性名称,但不是实际的列名。


更新:仅使用JDK获取当前日期的fromDate和toDate

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date fromDate = calendar.getTime();

calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Date toDate = calendar.getTime();

criteria.add(Restrictions.between("dateField", fromDate, toDate));

答案 1 :(得分:2)

喜欢这个吗?

criteria.add(Expression.eq("yourDate", aDate))

答案 2 :(得分:2)

fromDate.setTime(new Date());
fromDate.set(Calendar.HOUR_OF_DAY, 0);
fromDate.set(Calendar.MINUTE, 0);
fromDate.set(Calendar.SECOND, 0);
fromDate.set(Calendar.MILLISECOND, 0);

此代码非常危险...如果当天切换到夏令时(例如06.04.1980),您最终会遇到异常!!!

 java.lang.IllegalArgumentException: HOUR_OF_DAY: 0 -> 1day


HQL: from human pat where year(pat.birthdate) = :start_day and month(pat.birthdate) = :start_month and year(pat.birthdate) = :start_year ");

params.put("start_day", startDate.get(Calendar.DAY_OF_MONTH));
params.put("start_month", startDate.get(Calendar.MONTH) + 1);
params.put("start_year", startDate.get(Calendar.YEAR));

年/月/日函数使用基础数据库函数(extract,...)并仅比较这些值。因此,我不需要将时间设置为0,这导致上述异常。 我只是一个例子,我是如何解决这个问题的!也许有帮助

答案 3 :(得分:1)

最简单的方法是获取所有日期在给定日期的开始和结束之间的记录:

WHERE date BETWEEN :from AND :to

在Java代码中计算fromto

计算中午:

import static org.apache.commons.lang.time.DateUtils.ceiling;
import static org.apache.commons.lang.time.DateUtils.truncate;

Date someDay = new Date();
Date from = truncate(someDay, Calendar.DAY_OF_MONTH);
Date to = new Date(ceiling(someDay, Calendar.DAY_OF_MONTH).getTime() - 1);

答案 4 :(得分:0)

Restrictions.between("dateColumn", midnight1, midnight2)

答案 5 :(得分:0)

如何在Hibernate中做到这一点已经说过了。您可以使用例如以下方法在Java代码中准备Timestamp个对象:

Calendar cFrom = Calendar.getInstance();
cFrom.setTime(new Date()); /* today */
cFrom.set(Calendar.HOUR_OF_DAY, 0);
cFrom.set(Calendar.MINUTE, 0);
cFrom.set(Calendar.SECOND, 0);
cFrom.set(Calendar.MILLISECOND, 0);
Timestamp from = new Timestamp(cFrom.getTime().getTime());
Calendar cTo = Calendar.getInstance();
cTo.setTime(new Date()); /* today */
cTo.set(Calendar.HOUR_OF_DAY, 23);
cTo.set(Calendar.MINUTE, 59);
cTo.set(Calendar.SECOND, 59);
cTo.set(Calendar.MILLISECOND, 999);
Timestamp to = new Timestamp(cTo.getTime().getTime());

final String QUERY = ""
  + "SELECT tr "
  + "FROM Type tr "
  + "WHERE tr.timestamp >= :timestampFrom AND tr.timestamp <= :timestampTo";
Query query = entityManager.createQuery(QUERY);
query.setParameter("timestampFrom", from);
query.setParameter("timestampTo", to);
@SuppressWarnings("unchecked")
List<Type> ts = (List<Type>)query.getResultList();

答案 6 :(得分:0)

The following code will work

Calendar fromDate = Calendar.getInstance();
fromDate.setTime(new Date());
fromDate.set(Calendar.HOUR_OF_DAY, 0);
fromDate.set(Calendar.MINUTE, 0);
fromDate.set(Calendar.SECOND, 0);
fromDate.set(Calendar.MILLISECOND, 0);

Calendar toDate = Calendar.getInstance();
toDate.setTime(new Date());
toDate.set(Calendar.HOUR_OF_DAY, 23);
toDate.set(Calendar.MINUTE, 59);
toDate.set(Calendar.SECOND, 59);
toDate.set(Calendar.MILLISECOND, 999);

criteria.add(Restrictions.between("loadDate", fromDate.getTime(),
                    toDate.getTime()));

答案 7 :(得分:0)

//日期时间比较Hibernate 4.3

     Select c from Customer c where c.date<{d '2000-01-01'}