如何在休眠状态下比较日期
在我的数据库日期是 as 等,我需要检索在日期字段中具有当前日期的数据。我的意思是我只需要匹配。我如何使用休眠标准来做到这一点。2012-04-09 04:02:53
2012-04-09 04:04:51
2012-04-08 04:04:51
2012-04-09'
在我的数据库日期是 as 等,我需要检索在日期字段中具有当前日期的数据。我的意思是我只需要匹配。我如何使用休眠标准来做到这一点。2012-04-09 04:02:53
2012-04-09 04:04:51
2012-04-08 04:04:51
2012-04-09'
使用 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));
请注意,条件 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));
喜欢这个?
criteria.add(Expression.eq("yourDate", aDate))