Search code examples
javahibernatedatecriteriarestriction

Hibernate: how to get time gap between two Date columns?


For example, my columns look like

1990-02-13 00:00:00.0 and 2015-02-19 00:00:00.0.

How can I get the time gap (difference) in days?


Solution

  • If you are using MySQL dialect for Hibernate you can use the following functions,

    unix_timestamp(datetime_value) - to take the difference between two columns

    current_date() to get the current date. a list of reference functions could be found here

    https://hibernate.atlassian.net/secure/attachment/11953/MySQLDialect.java

    For the second question can you try using

    criteria.add( Restrictions.le("date_column", current_date() ) )