title: How to get sysdate, time and timestamp from database using hibernate
body: <p>In my application I need to get database date(<code>sysdate</code> in case of Oracle DB) and compare it with user input date (<em><code>String</code> converted to <code>java.util.Date</code></em>). From <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=961789" rel="nofollow">this forum</a> I got the following code which helps in the case of <strong>Oracle</strong> dialect.</p>
  
  <pre><code>public Date getDate() {
          Session session = getHibernateTemplate().getSessionFactory().openSession();
          SQLQuery query = session.createSQLQuery("select sysdate as mydate from dual");
          query.addScalar("mydate", Hibernate.TIMESTAMP);
          return (Date) query.uniqueResult();
      }
  </code></pre>
  
  <p>And from <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=974212&amp;view=previous" rel="nofollow">this link</a> got the following method which uses mapping file with formula.</p>
  
  <pre><code>&lt;property name="currentDate" formula="(select sysdate from dual)"/&gt;
  </code></pre>
  
  <p>Again this is specific to Oracle. I think using later method is more performance friendly, because we can get it from the same session, i.e no need of opening another session just for getting date.</p>
  
  <p>I am looking for a generic solution to get date, time and timestamp from any DBMS using Hibernate. Using HQL is the preferred. Hope such a solution is available.</p>