title: Date comparison using the JPA criteria API
body: <p>I got a range date picker with two dates: <code>start</code> and <code>end</code>, where both can be empty. I would like to filter a table, where the entity has exact one date: <code>date</code>.</p>
  
  <p>So, here are some examples. I'd like to match. Imaging the date to match is the current date (17/07/2016).</p>
  
  <ul>
  <li>null - 17/07/2016  -> match</li>
  <li>17/07/2016 - null  -> match</li>
  <li>17/07/2016 - 17/07/2016 -> match</li>
  <li>null - null -> match all</li>
  </ul>
  
  <p>Those are the edge cases in my opinion and the reason why I am struggling so much. </p>
  
  <p>Actually my code looks like: </p>
  
  <pre><code>CriteriaBuilder cb = getEm().getCriteriaBuilder();
  CriteriaQuery&lt;Transaction&gt; cq = cb.createQuery(Transaction.class);
  Root&lt;Transaction&gt; root = cq.from(Transaction.class);
  
  List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  
  Predicate startPredicate = cb.greaterThanOrEqualTo(root.get(Transaction_.date), start);
  Predicate endPredicate = cb.greaterThanOrEqualTo(root.get(Transaction_.date), end);
  
  predicates.add(startPredicate);
  predicates.add(endPredicate);
  cq.select(root).where(predicates.toArray(new Predicate[] {}));
  
  TypedQuery&lt;Transaction&gt; query = getEm().createQuery(cq);
  query.setFirstResult(firstRow);
  query.setMaxResults(maxRow);
  List&lt;Transaction&gt; resultList = query.getResultList();
  </code></pre>
  
  <p>I'd like to get a query like this: </p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM transaction
  WHERE ((cast(date AS DATE) &gt;= '2016-07-16' OR cast(date AS DATE) IS NULL))
         AND ((cast(date AS DATE) &lt;= '2016-07-17' OR cast(date AS DATE) IS NULL))
  </code></pre>
  
  <p>Please note: The static date is to simulate a start and end date. and <code>date</code> is the table column.</p>
  
  <p>I know that my code is wrong. It matches only ranges, without considering null values. Also, if start and end is the same day, I will get zero results. </p>
  
  <p>Do you have you any idea? How can I edit my code to match all the mentioned patterns?</p>