从假期列表中查找开始日期和结束日期之间的假期

时间:2014-01-15 05:21:53

标签: java hibernate

我有一个方法,它返回StartDate和End Date之间的假期列表。我在数据库中添加假期,所以假设数据库中有假期,如14/01 / 2014-16 / 01/2014。 因此,如果我从2014年1月15日开始搜索假期,它工作正常,但如果我从15-17开始搜索它会产生Exception,如IndexOutOfBoundException

public List<HolidayDTO> getHolidayBetweenDate(Date startDate, Date endDate) {
    Session session = null;
    List<HolidayDTO> holidayDtoList = null;
    try {
        session = this.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Holiday.class);
        List<Holiday> holidayList = criteria.add(
                Restrictions
                .between("holidayStartDate", startDate, endDate))
                .list();

        if (holidayList != null && holidayList.size() > 0) {
            holidayDtoList = new ArrayList<HolidayDTO>();
            Iterator<Holiday> holidayIterator = holidayList.iterator();
            while (holidayIterator.hasNext()) {
                Holiday holiday = holidayIterator.next();
                HolidayDTO holidayDto = new HolidayDTO();
                holidayDto.setHolidayId(holiday.getHolidayId());
                holidayDto.setHolidayName(holiday.
                        getHolidayName());
                holidayDto.setHolidayDuration(holiday. getHolidayDuration());
                holidayDto.setHolidayStartDate(holiday
                        .getHolidayStartDate());
                holidayDto.setHolidayEndDate(holiday.
                        getHolidayEndDate());
                StateDTO stateDto = new StateDTO();
                stateDto.setStateId(holiday.getState().
                        getStateId());
                stateDto.setStateName(holiday.getState()  .getStateName());
                CountryDTO countryDto = new CountryDTO();
                countryDto.setId(holiday.getState().
                        getCountry().getId());
                countryDto.setCountryCode(holiday.getState().
                        getCountry()
                        .getCountryCode());
                countryDto.setCountryName(holiday.getState().
                        getCountry()
                        .getCountryName());
                stateDto.setCountry(countryDto);
                holidayDto.setState(stateDto);
                holidayDtoList.add(holidayDto);
            }

        }
    } catch (Exception e) {
        session.getTransaction().rollback();
        e.printStackTrace();

    } finally {
        session.flush();
        session.close();
    }
    return holidayDtoList;
}

1 个答案:

答案 0 :(得分:0)

几天前我经历了同样的情况。我认为它与时间戳问题有关。

我遵循了相同的“&lt;&gt;”的替代方法旧学校解决方案。

criteria.add(Restrictions.ge("holidayStartDate", startDate)); 
criteria.add(Restrictions.lt("holidayStartDate", endDate));