如何将日期值与Hibernate条件中具有时间戳值的列相匹配?
我想将date如何将日期值与Hibernate条件中具有时间戳值的列相匹配?,hibernate,date,timestamp,criteria,Hibernate,Date,Timestamp,Criteria,我想将date2014-08-26值与具有时间戳数据类型的列(称为update_date)匹配 Criteria criteria=createEntityCriteria(Order.class); criteria.add(Restrictions.eq("updatedDate",transactionDate1)); 当我使用上述代码时,由于数据类型不匹配,它不会返回任何内容 select * from order_master where security_id=2 and clie
2014-08-26
值与具有时间戳数据类型的列(称为update_date)匹配
Criteria criteria=createEntityCriteria(Order.class);
criteria.add(Restrictions.eq("updatedDate",transactionDate1));
当我使用上述代码时,由于数据类型不匹配,它不会返回任何内容
select * from order_master where security_id=2 and client_master_id=3 and update_date="2014-08-26 19:04:14";
上面的查询返回准确的结果。主要问题是我只有数据2014-08-26
,但没有更新时间。
请告诉我如何在Hibernate条件中获得正确的结果
如下所示
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
// Create date 17-04-2011 - 00h00
Date minDate = formatter.parse(myDate);
// Create date 18-04-2011 - 00h00
// -> We take the 1st date and add it 1 day in millisecond thanks to a useful and not so known class
Date maxDate = new Date(minDate.getTime() + TimeUnit.DAYS.toMillis(1));
Conjunction and = Restrictions.conjunction();
// The order date must be >= 17-04-2011 - 00h00
and.add( Restrictions.ge("orderDate", minDate) );
// And the order date must be < 18-04-2011 - 00h00
and.add( Restrictions.lt("orderDate", maxDate) );
SimpleDataFormat格式化程序=新的SimpleDataFormat(“dd-MM-YYYY”);
字符串myDate=“17-04-2011”;
//创建日期2011年4月17日-00时00分
datemindate=formatter.parse(myDate);
//创建日期2011年4月18日-00时00分
//->由于一个有用且不太为人所知的类,我们将第一个日期加上一天(毫秒)
Date maxDate=新日期(minDate.getTime()+TimeUnit.DAYS.toMillis(1));
连接和=限制。连接();
//订单日期必须大于等于17-04-2011-00h00
添加(Restrictions.ge(“orderDate”,minDate));
//订单日期必须小于2011年4月18日-00时00分
添加(Restrictions.lt(“orderDate”,maxDate));
如下所示
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
// Create date 17-04-2011 - 00h00
Date minDate = formatter.parse(myDate);
// Create date 18-04-2011 - 00h00
// -> We take the 1st date and add it 1 day in millisecond thanks to a useful and not so known class
Date maxDate = new Date(minDate.getTime() + TimeUnit.DAYS.toMillis(1));
Conjunction and = Restrictions.conjunction();
// The order date must be >= 17-04-2011 - 00h00
and.add( Restrictions.ge("orderDate", minDate) );
// And the order date must be < 18-04-2011 - 00h00
and.add( Restrictions.lt("orderDate", maxDate) );
SimpleDataFormat格式化程序=新的SimpleDataFormat(“dd-MM-YYYY”);
字符串myDate=“17-04-2011”;
//创建日期2011年4月17日-00时00分
datemindate=formatter.parse(myDate);
//创建日期2011年4月18日-00时00分
//->由于一个有用且不太为人所知的类,我们将第一个日期加上一天(毫秒)
Date maxDate=新日期(minDate.getTime()+TimeUnit.DAYS.toMillis(1));
连接和=限制。连接();
//订单日期必须大于等于17-04-2011-00h00
添加(Restrictions.ge(“orderDate”,minDate));
//订单日期必须小于2011年4月18日-00时00分
添加(Restrictions.lt(“orderDate”,maxDate));
请参阅本帖-->
根据这篇文章,你不能使用Restrictions.eq()
或Restrictions.like()
来比较日期
以下是适用于您的代码:
要比较两个范围之间的日期,请执行以下操作:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String startDateStr = "18-08-2014";
String endDateStr = "27-08-2014";
Date startDate = formatter.parse(startDateStr);
Date endDate = formatter.parse(endDateStr);
List<Order> orders = session.createCriteria(Order.class)
.add(Restrictions.between("updatedDate", startDate, endDate))
.list();
for (Order order : orders) {
System.out.println(order.getUpdatedDate());
}
请参阅此帖子-->
根据这篇文章,你不能使用Restrictions.eq()
或Restrictions.like()
来比较日期
以下是适用于您的代码:
要比较两个范围之间的日期,请执行以下操作:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String startDateStr = "18-08-2014";
String endDateStr = "27-08-2014";
Date startDate = formatter.parse(startDateStr);
Date endDate = formatter.parse(endDateStr);
List<Order> orders = session.createCriteria(Order.class)
.add(Restrictions.between("updatedDate", startDate, endDate))
.list();
for (Order order : orders) {
System.out.println(order.getUpdatedDate());
}