Restrictions Between for Date in Hibernate Criteria

前端 未结 4 1507
情书的邮戳
情书的邮戳 2020-12-05 18:17

Hello I am using hibernate in my example.For bean Table Audit Trial I want to fetch audit trial between a date range with inclusion of upper & lower limits. My code is l

相关标签:
4条回答
  • 2020-12-05 18:54

    add one more day to end date

     @Autowired
     private SessionFactory sessionFactory;
    
     String startDate = "2019-07-31 ";
     String endDate = "2019-08-24 ";
    
     Date fromDate = format.parse(fromDate);
    
     /* Add one more day to the end date*/
    
     Date to =format.parse(endDate);
     Calendar today = Calendar.getInstance();
     today.setTime(dateto);
     today.add(Calendar.DAY_OF_YEAR, 1);
    
     Date toDate= format.parse(format.format(today.getTime()));
    
     Criteria crit = sessionFactory.getCurrentSession().createCriteria(model.class);
     crit.add(Restrictions.between("dateFieldName", fromDate, toDate));
     List result = crit.list();
    
    0 讨论(0)
  • 2020-12-05 18:55

    criteria.add(Restrictions.between("DATE(auditDate)", sDate, eDate)); use this for ignoring time from date.

    0 讨论(0)
  • 2020-12-05 19:07
     criteria.add(Restrictions.ge("fromDate", DateUtil.persianToGregorian(currentDate)));
     criteria.add(Restrictions.le("toDate",  DateUtil.persianToGregorian(currentDate)));
    
     return criteria.list();
    
    0 讨论(0)
  • 2020-12-05 19:14

    I assume your auditDate is in fact a timestamp. If it's the case, then this is normal, because 25/05/2011 means 25/05/2011 at 0 o'clock (in the morning). So, of course, every row having an audit timestamp in the date 25/05/2011 is after 0 o'clock in the morning.

    I would add 1 day to your end date, and use auditDate >= sDate and auditDate < eDate.

    criteria.add(Restrictions.ge("auditDate", sDate)); 
    criteria.add(Restrictions.lt("auditDate", eDate));
    
    0 讨论(0)
提交回复
热议问题