2017-09-15 62 views
0

我想使用標準API將登錄用戶的當前日期(根據他的時區)與表列進行比較。在java中使用Criteria Builder API的日期比較

但我無法從我爲用戶計算的本地日期中獲取表達式。我該如何解決這個問題?

public void checkTaskCategory(TaskCriteria criteria, CriteriaBuilder cb, CriteriaQuery<Task> q, Root<Task> task, List<Predicate> predicates) { 
    if (StringUtils.isNotBlank(criteria.getCategory())) { 
    LocalDate currentDateForUserTimezone = LocalDateTime.now(ZoneId.of("Pacific/Pago_Pago")).toLocalDate(); 
     if (StringUtils.equalsIgnoreCase(TaskConstants.TASK_OVERDUE, criteria.getCategory())) { 
      predicates.add(cb.greaterThan(cb.currentDate(), task.<Date>get(TaskConstants.DUE_DATE))); 
      predicates.add(cb.notEqual(task.get("status"), getStatus(Status.Value.DONE.toString()).getId())); 
      q.orderBy(cb.asc(task.get(TaskConstants.DUE_DATE))); 
     } 
    } 
} 

我要替換cb.currentDate()我以來cb.currentDate比較沒有考慮到時區計算currentDateForUserTimezone。我怎樣才能實現它?

+0

@lonut你知道嗎? –

+0

與您的問題無關,但您可以使用'LocalDate.now(ZoneId.of(「Pacific/Pago_Pago」))''。它相當於(但比它短)'LocalDateTime.now(ZoneId.of(「Pacific/Pago_Pago」))。toLocalDate()' – 2017-09-15 17:04:09

+0

@ Hugo ..謝謝。 –

回答

0

最後,我得到了解決方案。我們需要使用ParameterExpression以存在於數據庫中的列值與新的用戶定義的對象value.And比較,然後使用相同的別名中的setParameter使用該價值。代碼將改變這樣的事情:

然後在setParameter中使用別名「currentDate」,並在那裏設置值currentDateForUserTimezone,它將與所需的字段值進行比較。 }