仅将日期日期部分与Hibernate中的时间戳进行比较 [英] Compare date's date part only with Timestamp in Hibernate

查看:807
本文介绍了仅将日期日期部分与Hibernate中的时间戳进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有时间戳,应用程序中有日期。我喜欢以hibernate能够将所有与日期匹配的条目,而不是时间部分的方式编写hibernate条件。
例如

在DB时间戳中

2011-12-01 15:14:14

p>

在应用程序中,我确实有java.util.Date,它具有默认的时间部分。



我的问题是我使用下面的代码从数据库中搜索条目,但我什么也没有收到$ / $>

  DetachedCriteria criteria = DetachedCriteria.forClass(MyClass.class); 
criteria.add(Restrictions.like(TIMESTAMP_FIELD,javaUtilDate));
列表条目= this.getHibernateTemplate()。findByCriteria(criteria);

提前致谢

解决方案

如果您正在寻找按日期范围过滤(例如从今天到明天)的一般用法。
我这样使用它:



YourService.java

 来自Timestamp的日期= 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);
返回cal.getTime();
}

public static Date getTomorrowDate(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,1);
返回cal.getTime();
}

当然 - 每个代码总是有重构空间。


I have timestamp in database and in application i do have date . I like to write hibernate criteria in the way that hibernate can pull all entries those matches with date, not time part. e.g.

in DB timestamp

2011-12-01 15:14:14

and in application i do have java.util.Date which has by default time part.

my problem is when i search entries from database with following code i get nothing

    DetachedCriteria criteria = DetachedCriteria.forClass(MyClass.class);
    criteria.add(Restrictions.like(TIMESTAMP_FIELD, javaUtilDate));
    List entries =this.getHibernateTemplate().findByCriteria(criteria);

thanks in advance

解决方案

If you are looking for a general use of filtering by date range (eg. from today until tomorrow). I use it that way:

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();
}

And of course - there is always space for refactoring in every code.

这篇关于仅将日期日期部分与Hibernate中的时间戳进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆