gpt4 book ai didi

java - 使用 Hibernate Criteria 和 Oracle 处理时间的更好方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:28 27 4
gpt4 key购买 nike

我想按时间选择实体。我的 Oracle DBMS 的字段类型为 DATE,其中包含日期和时间。这是我的代码。如何按时间标准选择数据?

Calendar timeCal = new GregorianCalendar(0,0,0,0,0,0);
Date timeMin = timeCal.getTime();

timeCal.add(Calendar.HOUR_OF_DAY, 1);
Date timeMax = timeCal.getTime();

if (minDate != null && maxDate != null)
criteria.add(Restrictions.between("eventDate", minDate, maxDate));

if (onDate != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(onDate);
calendar.add(Calendar.DAY_OF_MONTH, 1);
criteria.add(Restrictions.between("eventDate", onDate, calendar.getTime()));
}

if(minTime!=null&&maxTime!=null){
/*
How to add Restriction on eventDate field, for time only?
if minTime is 3:00 and maxTime is 16:00, must to return
all rows having the time part of their eventDate between
3:00 and 16:00, regardless of the date part
*/
}

最佳答案

我找到了答案。只需要使用 sqlRestriction。

criteria.add(
Restrictions.sqlRestriction(
" NUMTODSINTERVAL(EVENT_DATE - TRUNC(EVENT_DATE), 'DAY') BETWEEN NUMTODSINTERVAL (?,'SECOND') AND NUMTODSINTERVAL (?,'SECOND')",
new Object[] { secondsForBegin, secondsForEnd },
new Type[] { StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER }));

关于java - 使用 Hibernate Criteria 和 Oracle 处理时间的更好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com