在我的数据库日期为2012-04-09 04:02:53
2012-04-09 04:04:51
2012-04-08 04:04:51
等,我需要在日期字段中检索具有当前日期的数据。我的意思是我只需要匹配2012-04-09'
。我如何使用休眠标准来做到这一点。
问问题
55213 次
8 回答
30
使用Restrictions.between()生成日期列在“2012-04-09 00:00:00”和“2012-04-09 23:59:59”之间的 where 子句
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));
于 2012-04-09T13:42:21.393 回答
2
像这样?
criteria.add(Expression.eq("yourDate", aDate))
于 2012-04-09T13:44:31.607 回答
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));
年/月/日函数使用底层 db 函数(提取、...)并仅比较这些值。因此,我不需要将导致上述异常的时间设置为 0。只是我如何解决问题的一个例子!也许它有帮助
于 2013-08-22T10:03:03.477 回答
1
最简单的方法是获取日期在给定日期的开始和结束之间的所有记录:
WHERE date BETWEEN :from AND :to
from
并to
在您的 Java 代码中进行计算。
对于计算午夜:
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);
于 2012-04-09T13:36:28.077 回答
1
如何在 Hibernate 中进行操作已经说过。您可以Timestamp
使用例如以下方法在 Java 代码中准备对象:
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();
于 2012-04-09T13:42:47.447 回答
0
Restrictions.between("dateColumn", midnight1, midnight2)
于 2012-04-09T13:39:34.747 回答
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()));
于 2013-04-23T10:40:47.723 回答
0
// 日期时间比较 Hibernate 4.3
Select c from Customer c where c.date<{d '2000-01-01'}
于 2018-12-06T21:37:37.020 回答