Java 从假日列表中查找开始日期和结束日期之间的假日
我有一个方法,返回开始日期和结束日期之间的假期列表。我在数据库中添加假期,所以假设数据库中有假期,如2014年1月14日至2014年1月16日。 所以,如果我从2014年1月15日开始搜索假期,它工作正常,但如果我从15-17日开始搜索,它会给出类似IndexOutOfBoundException的异常Java 从假日列表中查找开始日期和结束日期之间的假日,java,hibernate,Java,Hibernate,我有一个方法,返回开始日期和结束日期之间的假期列表。我在数据库中添加假期,所以假设数据库中有假期,如2014年1月14日至2014年1月16日。 所以,如果我从2014年1月15日开始搜索假期,它工作正常,但如果我从15-17日开始搜索,它会给出类似IndexOutOfBoundException的异常 public List<HolidayDTO> getHolidayBetweenDate(Date startDate, Date endDate) { Session s
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;
}
公共列表GetHolidayBetween日期(日期开始日期、日期结束日期){
会话=空;
List holidayDtoList=null;
试一试{
会话=this.getSessionFactory().openSession();
session.beginTransaction();
条件=session.createCriteria(Holiday.class);
List holidayList=criteria.add(
限制
.between(“holidayStartDate”,startDate,endDate))
.list();
if(holidayList!=null&&holidayList.size()>0){
holidayDtoList=新的ArrayList();
迭代器holidayIterator=holidayList.Iterator();
while(holidayIterator.hasNext()){
Holiday=holidayIterator.next();
HolidayDTO HolidayDTO=新HolidayDTO();
holidayDto.setHolidayId(holiday.getHolidayId());
HolidayTo.setHolidayName(假日)。
getHolidayName());
holidayDto.setHolidayDuration(holiday.getHolidayDuration());
HolidayTo.setHolidayStartDate(假日)
.getHolidayStartDate());
HolidayTo.setHolidayEndDate(假日)。
getHolidayEndDate());
StateDTO StateDTO=新StateDTO();
stateDto.setStateId(holiday.getState()。
getStateId());
stateDto.setStateName(holiday.getState().getStateName());
CountryDTO CountryDTO=新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);
添加(holidayDto);
}
}
}捕获(例外e){
session.getTransaction().rollback();
e、 printStackTrace();
}最后{
session.flush();
session.close();
}
返回度假酒店;
}
几天前我也遇到过同样的情况。我认为这与发行时间戳有关
对于相同的“老派”解决方案,我采用了另一种方法
criteria.add(Restrictions.ge("holidayStartDate", startDate));
criteria.add(Restrictions.lt("holidayStartDate", endDate));
请张贴你的stacktrace,因为不清楚你在问什么。