我想在数据库中搜索带有“ 2014-08-12”这样的一部分时间戳的所有行。如果有“ 2014-08-12 16:58:48.477”或“ 2014-08-12 15:58:48.477”之类的行,我想获取
我使用JPA + Hibernate + Criteria API
document.setRegister_date(DateUtil.DateToSQLTimestamp("2013-08-12 16:58:48.477"));
if (register_date != null) {
predicates.add(cb.equal(DocumentsRoot.get("register_date"), register_date));
}
答案 0 :(得分:2)
如果您知道格式将始终排除时间,则可以使用range query,在其中您可以将from
设置为00:00:00.001,并将to
设置为23:59: 59.999
答案 1 :(得分:0)
String date = "2013-08-12";
document.setRegister_date(DateUtil.DateToSQLTimestamp(date + " 00:00:00.000"));
document.setRegister_end_date(DateUtil.DateToSQLTimestamp(date + " 23:59:59.999"));
Timestamp register_date = document.getRegister_date();
Timestamp register_end_date = document.getRegister_end_date();
predicates.add(cb.between(DocumentsRoot.get("register_date"), register_date, register_end_date));