今天为了查询Oracle里面的Date的字段,但是Oracle储存的格式是yyyy-MM-dd HH:mm:ss 精确到秒。
而一般按日期查询都是yyyy-MM-dd 格式,最好解决了,如下分享一下传入yyyy-MM-dd 格式添加尾巴:
"+" 00:00:00" "+" 23:59:59" 就是查询当天的数据了:
public void testQuery1() {
Session session = null;
try {
session = HSFUtility.getSession();
session.beginTransaction();
List list = (List) session
.createQuery("select w.id ,w.name from WQ_VO w where w.findDate between :date and :date2 ")
.setDate("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2009-08-08"+" 00:00:00"))
.setDate("date2", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2009-08-08"+" 23:59:59"))
.list();
Iterator it = list.iterator();
while (it.hasNext()) {
Object[] obj = (Object[]) it.next();
System.out.println(obj[0] + "," + obj[1]);
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
throw new java.lang.RuntimeException();
} finally {
HSFUtility.closeSession();
}
}