I am trying to check if 'toDate' lies within the range , for that i tried 2 approaches but both haven't worked for me
approach 1:
if (MyEntity.getFromDate() != null
&& MyEntity.getToDate() != null)) {
predicate.getExpressions()
.add(criteriaBuilder.between((root.<Date>get("toDate")), MyEntity.getFromDate(),MyEntity.getToDate()));
approach 2
if(MyEntity.getFromDate() != null
&& MyEntity.getToDate() != null) {
DateFormat dateFormat = new SimpleDateFormat(Constants.DBDATEFORMAT);
String fromDateInString = dateFormat.format(MyEntity.getFromDate());
String toDateInString = dateFormat.format(MyEntity.getToDate());
String[] fromDateSplitation = fromDateInString.split(" ");
String[] toDateSplitation = toDateInString.split(" ");
StringBuilder startLimit = new StringBuilder();
StringBuilder endLimit = new StringBuilder();
startLimit.append(fromDateSplitation[0]).append(" ").append("00:00:00");
endLimit.append(toDateSplitation[0]).append(" ").append("23:59:59");
Date fromDate;
try {
fromDate = dateFormat.parse(startLimit.toString());
Date toDate = dateFormat.parse(endLimit.toString());
MyEntity.setFromDate(fromDate);
MyEntity.setToDate(toDate);
predicate.getExpressions().add(criteriaBuilder.between(root.get("toDate"),
MyEntity.getFromDate(), MyEntity.getToDate()));
} catch (ParseException e) {
e.printStackTrace();
}
Can anyone tell me how to properly use Between and Equal for date with criteria builder ?