如何获得30分钟的返回实例对象(Instance.now()-30分钟)

时间:2018-10-03 08:57:02

标签: java datetime java-time instant

private Instant submissionDate = Instant.now();


void scheduleResult() {
    if(submissionDate befor 30 min)
            execute;
}

如果submisisionDate早于30分钟,如何执行调度程序任务。

3 个答案:

答案 0 :(得分:2)

使用Instant#minus(30, ChronoUnit.MINUTES)

Instant submissionDate = ...

Instant now = Instant.now(); 
Instant thirtyMinutesBeforeNow = now.minus(30, ChronoUnit.MINUTES);

if (submissionDate.isBefore(thirtyMinutesBeforeNow)) {

}

Instant#minusSeconds(1800)

Instant submissionDate = ...

Instant now = Instant.now(); 
Instant thirtyMinutesBeforeNow = now.minusSeconds(1800);

if (submissionDate.isBefore(thirtyMinutesBeforeNow)) {

}

答案 1 :(得分:1)

您可以在过去的日期中激活30分钟:

LocalDateTime halfHourInPast = LocalDateTime.now().minusMinutes(30);

并使用它来比较您的日期:

LocalDateTime submissionDate = LocalDateTime.ofInstant(submissionDateInstant, ZoneOffset.UTC);
if(submissionDate.isBefore(halfHourInPast)){
   //schedule
}

答案 2 :(得分:1)

if ( submissionDate.isBefore(Instant.now().minus(30, ChronoUnit.MINUTES))){
//                      run
}