如果CriteriaBuilder
有办法知道两个日期是否少于构建我的构建器的12个月时间吗?
Expression<LocalDate> expDate1= root.get("date1");
LocalDate date1 = LocalDate.parse(expDate1.toString());
Expression<LocalDate> expDate2 = root.get("date2");
LocalDate date2 = LocalDate.parse(expDate2.toString());
Long monthBetween = ChronoUnit.MONTHS.between(date1, date2);
Predicate predicate= cb.and(monthBetween < 12); // doesnt accept is not a expression
答案 0 :(得分:1)
这不是构建标准的正确方法。使用方法lessThan(..)替换<
运算符。方法lt(..)也是如此。
请注意,表达式monthBetween < 12
本身会返回boolean
,但方法and(..)会接受谓词作为参数。
Predicate predicate = cb.and(cb.lessThan(monthBetween, 12));