Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to convert from String to Calendar,but always it is giving the exception.

Here is my code:-

public List<ClassA> getData(String fromDateTime, String toDateTime, Integer apiId)
            throws DAOException, ParseException {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal1.setTime(df.parse(fromDateTime));
        cal2.setTime(df.parse(toDateTime));
return findByCriteria(Restrictions.eq("apiId", apiId),
                Restrictions.between("searchGenerationTime", fromDateTime, toDateTime));       
}

please someone can told me where i am wrong in this code ?? lets assume fromDateTime & toDateTime are user input as 2014-04-02 19:24:43 & 2014-04-03 12:03:51 respectively. Thanks.

Edited:- Here is my stack trace

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Calendar
    at org.hibernate.type.descriptor.java.CalendarTypeDescriptor.unwrap(CalendarTypeDescriptor.java:40)
    at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:53)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
    at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1873)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1844)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
    at org.hibernate.loader.Loader.doQuery(Loader.java:801)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2542)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at com.via.database.util.DAOUtil.findByCriteriaInternal(DAOUtil.java:227)
    at com.via.database.util.DAOUtil.findByCriteria(DAOUtil.java:198)
    at com.via.database.dao.hibernate.GenericHibernateDAO.findByCriteria(GenericHibernateDAO.java:175)
    at com.eos.accounts.database.dao.hibernate.HotelSearcherRequestResponseDao.getData(HotelSearcherRequestResponseDao.java:53)
    at com.eos.hotels.HotelSearcherUtility.getHotelSearchRequestResopnseData(HotelSearcherUtility.java:50)
    at com.eos.b2c.ui.B2cCallcenterNavigation.doGetExtension(B2cCallcenterNavigation.java:9568)
    at com.eos.b2c.ui.B2cCallcenterNavigation.doGet(B2cCallcenterNavigation.java:5851)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.eos.accounts.UserManagerFilter.doFilter(UserManagerFilter.java:972)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:701)
share|improve this question
    
Can you post the stack trace? –  Bhesh Gurung Apr 6 at 16:28
    
Find the faulty line, using the stack trace, and post that. –  keyser Apr 6 at 16:29
6  
The stack trace and code you posted are completely unrelated. –  Sotirios Delimanolis Apr 6 at 16:29
    
I have posted my whole function. –  vicky Apr 6 at 16:30
1  
What's the point of cal1 and cal2 if you aren't going to use them in the Restrictions. –  Sotirios Delimanolis Apr 6 at 16:34
show 1 more comment

2 Answers

I think the return statement should be as follows:

return findByCriteria(Restrictions.eq("apiId", apiId),
               Restrictions.between("searchGenerationTime", cal1, cal2)); 
share|improve this answer
add comment

After analyzing exception stack trace, I found that entity ClassA has a Calendar field searchGenerationTime mapped to SQL timestamp column.

For this case, you are allowed to pass only arguments of these classes:

  • java.util.Calendar
  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp
  • java.util.Date

You can see this in hibernate source code.

In fact, where from Hibernate knows about your date format ("yyyy-MM-dd HH:mm:ss") ?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.