简体   繁体   English

休眠日期限制

[英]hibernate restriction on date issue

I am facing issue in hibernate criteria restriction. 我在休眠标准限制方面面临问题。 I have data in db2 table as 2014-03-05 20:07:33 and I am trying to fetch data based on the below criteria: 我在db2表中有2014-03-05 20:07:33的数据,并且我尝试根据以下条件来获取数据:

hibernateCriteria.add(Restrictions.ge("eventTs",
            getLowerBoundDate(adminReportsBeanDTO.getFromDate())));
    hibernateCriteria.add(Restrictions.le("eventTs",
            getUpperBoundDate(adminReportsBeanDTO.getToDate())));
public static Date getLowerBoundDate(Date fromDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(fromDate);
    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();
}

/**
 * 
 * @param fromDate
 * @return
 */
public static Date getUpperBoundDate(Date toDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(toDate);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 999);
    return cal.getTime();
}

and from date is as Wed Mar 05 00:00:00 IST 2014 and to date is Wed Mar 05 23:59:59 IST 2014 日期为IST 2014年3月5日星期三00:00:00,日期为IST 2014年3月5日星期三23:59:59

and I have records in the table for the above date but it is not displaying. 并且我在表格中有上述日期的记录,但未显示。 But the report is displaying for the next date 6th march. 但报告显示的日期为3月6日。

here is my hibernate criteria 这是我的冬眠标准

CriteriaImpl(com.gtech.lsp.beans.Audit:this[][eventTs>=Wed Mar 05 00:00:00 IST 2014,            eventTs<=Wed Mar 05 23:59:59 IST 2014])

Please help me on this. 请帮我。

It looks like you're comparing dates in different timezones! 您似乎正在比较不同时区的日期! Your Java Calendar are set in Indian timezone (IST). 您的Java日历设置为印度时区(IST)。 What about the dates in database? 那么数据库中的日期呢? Try setting your Calendar timezone to GMT if this is your database timezone: 如果这是您的数据库时区,请尝试将日历时区设置为GMT:

TimeZone tz = TimeZone.getTimeZone("GMT");
cal.setTimeZone(tz);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM