Working with Date in Mysql, JPA

0

I'm using JPA and creating an entity to save a date within the database. So far so good, the problem is that I need to display the bank information according to the month of my date . Does it have a way of when to ask JPQL to return only with the selected month? Example:

public void exemplo(Date data){     
    String jpql = "select t from time t where t.data.getmes() = cData"
    Query query = em.createQuery(jpql);

    query.setParameter("cData", data);
}
    
asked by anonymous 14.08.2017 / 20:11

1 answer

1

At first you can make a change in your query to check only the month, using the MONTH method. See:

select t from time t where MONTH(t.data.getmes()) = cData

Then you would need to redeem the month that is passing in your variable data as a parameter using Calendar ". See:

Calendar cal = Calendar.getInstance();
cal.setTime(data);
int month = cal.get(Calendar.MONTH);

query.setParameter("cData", month);

Result:

public void exemplo(Date data){     
    String jpql = "select t from time t where MONTH(t.data.getmes()) = cData"
    Query query = em.createQuery(jpql);

    Calendar cal = Calendar.getInstance();
    cal.setTime(data);
    int month = cal.get(Calendar.MONTH);

    query.setParameter("cData", month);
}
    
14.08.2017 / 20:41