Home » Java » java – How to check differences between dates in Hibernate-Exceptionshub

java – How to check differences between dates in Hibernate-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to select objects from a database, checking a date label. Object should be selected only if 3 hours has passed from the value of this label. For example, a value of 2020-02-17 17:32:24, should be selected only if current time is at least 2020-02-17 20:32:24.

    Date now = new Date();
    long nowLong =  3600000L * HOURS;

    //NOT important, just connection.
     Session session = GestorBBDD.getSession();
    CriteriaBuilder cb = session.getCriteriaBuilder();
    CriteriaQuery<ImageDemand> cr = cb.createQuery(ImageDemand.class);
    Root<ImageDemand> root = cr.from(ImageDemand.class);

    //COMPARE HERE
    cr.select(root).where(cb.(root.get("time"), ???));

Maybe root.get(“time”) * nowLong? root.get(“time”) – now, 360000 * 3?
Or maybe cr.select(root).where(cb.greaterThan(root.get(“time”) – (root.get(“time”) + nowLong), 0));

But nothing of these can compile. Any idea?

Thank you.

How to&Answers: