Java util 日期与条件 api 的比较未按预期工作



我正在尝试使用CriteriaBuilderbetween从数据库中过滤掉一些数据。我正在使用java.util.Date作为我的输入和实体列。我总是按照输入日期获得结果 - 1。例如,如果我的输入日期是 16-12-2019,那么我也会收到 15-12-2019 的数据。我无法理解这种奇怪的行为。我使用以下方法:

predicates.add(criteriaBuilder.between(root.get(Constants.COL_REGISTRATION_DATE),
registrationStartDate, registrationEndDate));

我也尝试了下面的方法,但结果仍然相同。

Predicate startDatePredicate = criteriaBuilder.greaterThanOrEqualTo(root.get(Constants.COL_REGISTRATION_DATE), registrationStartDate);
Predicate endDatePredicate = criteriaBuilder.lessThanOrEqualTo(root.get(Constants.COL_REGISTRATION_DATE), registrationEndDate);
predicates.add(criteriaBuilder.and(startDatePredicate, endDatePredicate));

有没有人遇到过这样的问题。请帮我解决这个问题。

请尝试:

predicates.add(criteriaBuilder.between(root.get(Constants.COL_REGISTRATION_DATE),
LocalDate.parse(registrationStartDate), 
LocalDate.parse(registrationEndDate))
);

相关内容

最新更新