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>
逻辑说明
- 未来日期判断:用当前时间调用
isBefore()方法和wcdate比较,返回true就说明wcdate在未来 - 今天/过去日期判断:用当前时间调用
isAfter()或isEqual()和wcdate比较,满足任意一个就说明wcdate不是未来日期 - 一周以前判断:先把当前时间减去7天(
minusWeeks(1)),再用这个时间和wcdate比较,如果减完的时间比wcdate晚,就说明wcdate已经是一周以前的日期了
小提示:如果使用
java.util.Date类型,一周以前的判断写法为${timesheet.wcdate.before(#dates.addDays(#dates.createNow(), -7))},其他逻辑对应调整即可。
内容的提问来源于stack exchange,提问作者MindGames2015
相关产品推荐
相关产品推荐

