最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

criteria和oracle数据库使用,java

IT圈 admin 40浏览 0评论

criteria和oracle数据库使用,java

我想按时间选择实体.

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

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

标签:oracle,hibernate,criteria,java,time

来源: .html

criteria和oracle数据库使用,java

我想按时间选择实体.

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

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

标签:oracle,hibernate,criteria,java,time

来源: .html

发布评论

评论列表 (0)

  1. 暂无评论