You need to enable JavaScript to run this app. Thymeleaf实现timesheet表wcdate未来/过去日期4种场景判断咨询
优惠活动
大模型
产品
解决方案
定价
更多

Thymeleaf实现timesheet表wcdate未来/过去日期4种场景判断咨询

前提说明

  • 以下代码默认你的wcdate字段为java.time.LocalDate类型,若使用旧版java.util.Date,只需将代码中#temporals替换为#dates即可
  • 所有日期比较默认忽略时分秒,仅按自然日计算

完整修正代码

<!-- 场景1:id为0且wcdate是未来日期,展示AVAILABLE -->
<span th:if="${timesheet.id == 0 and #temporals.createNow().isBefore(timesheet.wcdate)}"
      class="available">AVAILABLE</span>

<!-- 场景2:id为0且wcdate是今天或更早日期,展示MISSING -->
<span th:if="${timesheet.id == 0 and (#temporals.createNow().isAfter(timesheet.wcdate) or #temporals.createNow().isEqual(timesheet.wcdate))}"
      class="missing">MISSING</span>

<!-- 场景3:id不为0且wcdate距今不足一周,展示DRAFT -->
<span th:if="${timesheet.id != 0 and #temporals.createNow().minusWeeks(1).isBefore(timesheet.wcdate)}"
      class="draft">DRAFT</span>

<!-- 场景4:id不为0且wcdate距今超过一周,展示OVERDUE -->
<span th:if="${timesheet.id != 0 and #temporals.createNow().minusWeeks(1).isAfter(timesheet.wcdate)}"
      class="overdue">OVERDUE</span>

逻辑说明

  1. 未来日期判断:用当前时间调用isBefore()方法和wcdate比较,返回true就说明wcdate在未来
  2. 今天/过去日期判断:用当前时间调用isAfter()或isEqual()和wcdate比较,满足任意一个就说明wcdate不是未来日期
  3. 一周以前判断:先把当前时间减去7天(minusWeeks(1)),再用这个时间和wcdate比较,如果减完的时间比wcdate晚,就说明wcdate已经是一周以前的日期了

小提示:如果使用java.util.Date类型,一周以前的判断写法为${timesheet.wcdate.before(#dates.addDays(#dates.createNow(), -7))},其他逻辑对应调整即可。

内容的提问来源于stack exchange,提问作者MindGames2015

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 17:42:00