try{
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
String s1="2009-04-01";
String s2="2009-04-10";
Date startDate = dateFormat.parse(s1);//util类型
Date endDate = dateFormat.parse(s2);
//把年月日日期字符串转换成Date格式的方法是用SimpleDateFormat,但是默认有时分秒.
Criteria c=s.createCriteria(UserBusiness.class);
c.add(Expression.between("month", startDate, endDate));
//这几天关键就在想怎么传入这个Date
result=c.list();
Iterator it=result.iterator();
while(it.hasNext()){
UserBusiness us=(UserBusiness)it.next();
System.out.println(us.getUserid());
System.out.println(us.getBusinessId());
System.out.println(us.getMonth());
}
}catch(HibernateException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
String s1="2009-04-01";
String s2="2009-04-10";
Date startDate = dateFormat.parse(s1);//util类型
Date endDate = dateFormat.parse(s2);
//把年月日日期字符串转换成Date格式的方法是用SimpleDateFormat,但是默认有时分秒.
Criteria c=s.createCriteria(UserBusiness.class);
c.add(Expression.between("month", startDate, endDate));
//这几天关键就在想怎么传入这个Date
result=c.list();
Iterator it=result.iterator();
while(it.hasNext()){
UserBusiness us=(UserBusiness)it.next();
System.out.println(us.getUserid());
System.out.println(us.getBusinessId());
System.out.println(us.getMonth());
}
}catch(HibernateException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
本文介绍了一种利用Hibernate框架结合SimpleDateFormat类来实现从数据库中查询特定日期范围内数据的方法。通过创建Criteria对象并使用between方法指定起始与结束日期,可以有效地筛选出所需的数据记录。
1321

被折叠的 条评论
为什么被折叠?



