Reputation: 8498
I have one table which has a column submitted_date(time-stamp without timezone). I need to list all records in the table having a particular date as submitted date. But do not consider the time in database. I retrieve the records by using criteria query and hibernate. How to ignore the time here?
Actually I pass a date from client side and has to retrieve the records that have the same date as submitted_date. But no need to consider the time.
else if(extjsFilter.getField().equals("submittedDate")) {
String str_date=extjsFilter.getValue();
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = format1.parse(str_date);
String datenew = format2.format(date2);
Date date = (Date)format2.parse(datenew);
if(extjsFilter.getType().equals("date"))
{
if(extjsFilter.getComparison().equals("gt"))
{
Filter postDateFilterGT = getSession().enableFilter("jobFilterPostDateGT");
postDateFilterGT.setParameter("postDateFilterGT", date);
}
if(extjsFilter.getComparison().equals("lt"))
{
Filter postDateFilterLT = getSession().enableFilter("jobFilterPostDateLT");
postDateFilterLT.setParameter("postDateFilterLT", date);
}
if(extjsFilter.getComparison().equals("eq"))
{
Filter postDateFilterEQ = getSession().enableFilter("jobFilterPostDateEQ");
postDateFilterEQ.setParameter("postDateFilterEQ", date);
}
}
}
Above is my code. Client side is done using extjs. The server side code of extjs filtering for one date field is this.
hibernate is given below.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hiringsteps.ats.job.domain">
<class
name="Job"
table="hs_job_master">
<id name="id" column="job_id" unsaved-value="null">
<generator class="sequence">
<param name="sequence">hs_job_id_seq</param>
</generator>
</id>
<property name="submittedDate" column="submitted_date"/>
<filter name="jobFilterPostDateGT"><![CDATA[submitted_date > :postDateFilterGT]]></filter>
<filter name="jobFilterPostDateLT"><![CDATA[submitted_date < :postDateFilterLT]]></filter>
<filter name="jobFilterPostDateEQ"><![CDATA[:postDateFilterEQ = submitted_date]]></filter>
</class>
<filter-def name="jobFilterPostDateGT">
<filter-param name="postDateFilterGT" type="date"/>
</filter-def>
<filter-def name="jobFilterPostDateLT">
<filter-param name="postDateFilterLT" type="date"/>
</filter-def>
<filter-def name="jobFilterPostDateEQ">
<filter-param name="postDateFilterEQ" type="date"/>
</filter-def>
</hibernate-mapping>
I have two records in database having submitted_date as below.
2013-02-15 00:00:00
2013-02-15 13:04:42.787
When I make a query to filter records having date as today, the first record with submitted date 2013-02-15 00:00:00 is only retrieved.
this is because the date object I used to query, has also this value '2013-02-15 00:00:00'
How will I make a query which will ignore the time part?
Upvotes: 2
Views: 22176
Reputation: 11602
You can try below code, which utilizes native Oracle trunc
function to discard time while comparison.
criteria.add(Restrictions.sqlRestriction("trunc(submitted_date) = ?", submittedDate, org.hibernate.type.StandardBasicTypes.DATE));
You can modify the function according to the database provider.
Upvotes: 5
Reputation: 379
For this, you will have to apply a restriction that selects all dates between submittedDate and submittedDate+1.
//Resticts the dates between start date 0000 hrs and end date 0000 hrs
criteria.add(Restrictions.ge("startDate", sDate));
criteria.add(Restrictions.lt("endDate", eDate));
In your case, startDate = 2013-02-15 and end date = 2013-02-16
Upvotes: 5
Reputation: 1760
Simply add a restriction to your criteria for property corresponding to column submitted_date. You can write it as
criteria.add(Restrictions.eq("submittedDate"), '2013-02-15');
Upvotes: 1