Criteria API: Correct Date Format










0















I am making a search module that will be able to filter data according to some criteria.



I already made the following:



public class SearchModuleBean implements Serializable

@PersistenceContext
EntityManager entityManager;

private Date departureDate;
private Date returnDate;

public List<Flight> search()
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Flight> criteriaQuery = cb.createQuery(Flight.class);
Root<Flight> c = criteriaQuery.from(Flight.class);
criteriaQuery.select(c).where(getPredicates(cb, c).toArray(new Predicate[0]));
TypedQuery<Flight> query = entityManager.createQuery(criteriaQuery);
System.out.println(query.getResultList());
return query.getResultList();


private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c)
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(c.get(Flight_.departureDate), departureDate));
return predicates;




Getters/Setters/Annotations and imports are not displayed here.



I have a JSF page which fills in the departure- and arrivalDates.
In the getPredicates method I then check for the departureDate if it is equal to Dates in our database. The issue is that they have a different format so he never finds anything.










share|improve this question



















  • 2





    A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

    – Billy Frost
    Nov 13 '18 at 14:55











  • Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

    – tom
    Nov 13 '18 at 15:03











  • This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

    – Ayoub Rossi
    Nov 13 '18 at 15:33















0















I am making a search module that will be able to filter data according to some criteria.



I already made the following:



public class SearchModuleBean implements Serializable

@PersistenceContext
EntityManager entityManager;

private Date departureDate;
private Date returnDate;

public List<Flight> search()
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Flight> criteriaQuery = cb.createQuery(Flight.class);
Root<Flight> c = criteriaQuery.from(Flight.class);
criteriaQuery.select(c).where(getPredicates(cb, c).toArray(new Predicate[0]));
TypedQuery<Flight> query = entityManager.createQuery(criteriaQuery);
System.out.println(query.getResultList());
return query.getResultList();


private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c)
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(c.get(Flight_.departureDate), departureDate));
return predicates;




Getters/Setters/Annotations and imports are not displayed here.



I have a JSF page which fills in the departure- and arrivalDates.
In the getPredicates method I then check for the departureDate if it is equal to Dates in our database. The issue is that they have a different format so he never finds anything.










share|improve this question



















  • 2





    A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

    – Billy Frost
    Nov 13 '18 at 14:55











  • Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

    – tom
    Nov 13 '18 at 15:03











  • This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

    – Ayoub Rossi
    Nov 13 '18 at 15:33













0












0








0








I am making a search module that will be able to filter data according to some criteria.



I already made the following:



public class SearchModuleBean implements Serializable

@PersistenceContext
EntityManager entityManager;

private Date departureDate;
private Date returnDate;

public List<Flight> search()
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Flight> criteriaQuery = cb.createQuery(Flight.class);
Root<Flight> c = criteriaQuery.from(Flight.class);
criteriaQuery.select(c).where(getPredicates(cb, c).toArray(new Predicate[0]));
TypedQuery<Flight> query = entityManager.createQuery(criteriaQuery);
System.out.println(query.getResultList());
return query.getResultList();


private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c)
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(c.get(Flight_.departureDate), departureDate));
return predicates;




Getters/Setters/Annotations and imports are not displayed here.



I have a JSF page which fills in the departure- and arrivalDates.
In the getPredicates method I then check for the departureDate if it is equal to Dates in our database. The issue is that they have a different format so he never finds anything.










share|improve this question
















I am making a search module that will be able to filter data according to some criteria.



I already made the following:



public class SearchModuleBean implements Serializable

@PersistenceContext
EntityManager entityManager;

private Date departureDate;
private Date returnDate;

public List<Flight> search()
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Flight> criteriaQuery = cb.createQuery(Flight.class);
Root<Flight> c = criteriaQuery.from(Flight.class);
criteriaQuery.select(c).where(getPredicates(cb, c).toArray(new Predicate[0]));
TypedQuery<Flight> query = entityManager.createQuery(criteriaQuery);
System.out.println(query.getResultList());
return query.getResultList();


private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c)
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(c.get(Flight_.departureDate), departureDate));
return predicates;




Getters/Setters/Annotations and imports are not displayed here.



I have a JSF page which fills in the departure- and arrivalDates.
In the getPredicates method I then check for the departureDate if it is equal to Dates in our database. The issue is that they have a different format so he never finds anything.







java jpa criteria-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:54







Ayoub Rossi

















asked Nov 13 '18 at 14:19









Ayoub RossiAyoub Rossi

357




357







  • 2





    A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

    – Billy Frost
    Nov 13 '18 at 14:55











  • Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

    – tom
    Nov 13 '18 at 15:03











  • This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

    – Ayoub Rossi
    Nov 13 '18 at 15:33












  • 2





    A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

    – Billy Frost
    Nov 13 '18 at 14:55











  • Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

    – tom
    Nov 13 '18 at 15:03











  • This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

    – Ayoub Rossi
    Nov 13 '18 at 15:33







2




2





A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

– Billy Frost
Nov 13 '18 at 14:55





A Date in Criteria is not in any "format", it is a Java Date object. If you have a criteria query that you have problems with the first step would be to look at the SQL generated, and the column type in the datastore

– Billy Frost
Nov 13 '18 at 14:55













Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

– tom
Nov 13 '18 at 15:03





Billy Frost is correct, one thing to check would be if the Dates are really equal in Java and in the database. java.util.Date can contain time information, if you're database only contains date-informationen they might not match (11-13-2018 12:00:00 != 11-13-2018 00:00:00).

– tom
Nov 13 '18 at 15:03













This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

– Ayoub Rossi
Nov 13 '18 at 15:33





This is correct and I knew that, the issue is that they indeed do not match because of the time. Can I only check for the date even if I have date and time in my database?

– Ayoub Rossi
Nov 13 '18 at 15:33












1 Answer
1






active

oldest

votes


















0














I don't tend to use the Criteria API and there may be another way but you could possibly use the between operator as below:



private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c, 
Date departureDate)

List<Predicate> predicates = new ArrayList<>();

//remove time portion from specified date: now dd/mm/yy 00:00
Date startDate = DateUtils.truncate(departureDate, Calendar.DATE);

//new date with time initialized to 23:59:59
Date endDate = DateUtils.addSeconds(DateUtils.addDays(startDate, 1), - 1);

predicates.add(cb.between(c.get(Flight_.departureDate), startDate, endDate));

return predicates;



DateUtils is a class in Commons Lang:



<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283074%2fcriteria-api-correct-date-format%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I don't tend to use the Criteria API and there may be another way but you could possibly use the between operator as below:



    private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c, 
    Date departureDate)

    List<Predicate> predicates = new ArrayList<>();

    //remove time portion from specified date: now dd/mm/yy 00:00
    Date startDate = DateUtils.truncate(departureDate, Calendar.DATE);

    //new date with time initialized to 23:59:59
    Date endDate = DateUtils.addSeconds(DateUtils.addDays(startDate, 1), - 1);

    predicates.add(cb.between(c.get(Flight_.departureDate), startDate, endDate));

    return predicates;



    DateUtils is a class in Commons Lang:



    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
    </dependency>





    share|improve this answer





























      0














      I don't tend to use the Criteria API and there may be another way but you could possibly use the between operator as below:



      private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c, 
      Date departureDate)

      List<Predicate> predicates = new ArrayList<>();

      //remove time portion from specified date: now dd/mm/yy 00:00
      Date startDate = DateUtils.truncate(departureDate, Calendar.DATE);

      //new date with time initialized to 23:59:59
      Date endDate = DateUtils.addSeconds(DateUtils.addDays(startDate, 1), - 1);

      predicates.add(cb.between(c.get(Flight_.departureDate), startDate, endDate));

      return predicates;



      DateUtils is a class in Commons Lang:



      <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.8.1</version>
      </dependency>





      share|improve this answer



























        0












        0








        0







        I don't tend to use the Criteria API and there may be another way but you could possibly use the between operator as below:



        private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c, 
        Date departureDate)

        List<Predicate> predicates = new ArrayList<>();

        //remove time portion from specified date: now dd/mm/yy 00:00
        Date startDate = DateUtils.truncate(departureDate, Calendar.DATE);

        //new date with time initialized to 23:59:59
        Date endDate = DateUtils.addSeconds(DateUtils.addDays(startDate, 1), - 1);

        predicates.add(cb.between(c.get(Flight_.departureDate), startDate, endDate));

        return predicates;



        DateUtils is a class in Commons Lang:



        <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
        </dependency>





        share|improve this answer















        I don't tend to use the Criteria API and there may be another way but you could possibly use the between operator as below:



        private List<Predicate> getPredicates(CriteriaBuilder cb, Root<Flight> c, 
        Date departureDate)

        List<Predicate> predicates = new ArrayList<>();

        //remove time portion from specified date: now dd/mm/yy 00:00
        Date startDate = DateUtils.truncate(departureDate, Calendar.DATE);

        //new date with time initialized to 23:59:59
        Date endDate = DateUtils.addSeconds(DateUtils.addDays(startDate, 1), - 1);

        predicates.add(cb.between(c.get(Flight_.departureDate), startDate, endDate));

        return predicates;



        DateUtils is a class in Commons Lang:



        <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
        </dependency>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 16:26

























        answered Nov 13 '18 at 15:52









        Alan HayAlan Hay

        15.4k22769




        15.4k22769



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283074%2fcriteria-api-correct-date-format%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3