java - Spring , hibernate : Check for timestamp within expiration time

标签 java spring hibernate spring-mvc hql

我正在使用 Hibernate 和 DB 作为 Postgres 开发一个 Spring-MVC 项目,其中可以将任务分配给属于某个组的个人。

现在任务有结束时间,我有一个运行的调度程序,每 30 分钟检查一次任务是否还有大约 48 小时要完成,如果是这样,我会发送一封电子邮件。

不幸的是,我不能只检查少于 48 小时,因为我也在检查少于 12 小时和少于 8 小时。

所以,我想为什么不检查剩余时间是否在 47-48 小时之间。而且我不知道如何检查这种情况。

所以,基本上,从当前时间开始,如果该行的时间戳远在 47-48 小时之后,我想检索该行。 我知道如何检查现在的内容,下面是代码。任何人都可以帮助我如何在 47-48 小时内检查。非常感谢。

代码:

   @Override
       public List<GroupNotes> findGroupNotesWithExpiryInFortyEightHours() {
            int val = 0;
            Session session = this.sessionFactory.getCurrentSession();
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            Query query = session.createQuery("from GroupNotes as gn where gn.zugwisenPersonId!=:val and gn.timeToend>:timestamp");

        }

非常感谢。

最佳答案

这个查询应该可以完成这项工作(虽然没有测试过,我这里没有 IDE):

long currentTime = System.currentTimeMillis();
long plus47Hours = currentTime + (47 * 60 * 60 * 1000);
Timestamp plus47HoursTS = new Timestamp(plus47Hours);

long plus48Hours = currentTime + (48 * 60 * 60 * 1000);
Timestamp plus48HoursTS = new Timestamp(plus48Hours);

Query query = session.createQuery("from GroupNotes as gn where gn.zugwisenPersonId!=:val and gn.timeToend > :from and gn.timeToend < :to");
query.setParameter("from", plus47HoursTS);
query.setParameter("to", plus48HoursTS);

关于java - Spring , hibernate : Check for timestamp within expiration time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611390/

相关文章:

java - I/O 相对路径

spring - JPA 外键不是对象

java - 如何使用composite-id类元素?

java - 如何为一个实体设置多个数据库? [ hibernate ]

java - JPanel 中透明 JCheckBox 的错误?

java - 编写一个输入验证循环,要求用户输入 “Yes” 或 “No”

java - 问题在面板数组中的 JButton 上设置约束

java - 创建名为 'entityManagerFactory' 的 bean 时出错 -> 无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元

java - Apache Spark 和 Spring Boot 的依赖冲突

hibernate - QueryDsl 交叉联接返回零个元素