Thymeleaf。如何比较日期是来自未来还是过去?

时间:2018-04-20 16:34:12

标签: spring thymeleaf

我想检查一下Employee.dateOfTermination中的日期是否在日期"现在"之后。不幸的是,在运行以下代码后,我对数据一无所知:

<td> <div th:switch="${employee.dateOfTermination}"> 
                            <span th:case="'&lt; now'">CASE 1</span>
                            <span th:case="'&gt; now'" th:text="${#dates.format(employee.dateOfTermination, 'dd-MM-yyyy')}">CASE 2</span></div></td>

问题出现了,百里香的语法似乎对我来说似乎无法容忍和恶心。我尝试过:if,并解析一个int。

1 个答案:

答案 0 :(得分:4)

我会这样做:

<span th:if="${employee.dateOfTermination.before(#dates.createNow())}">Case 1</span>
<span th:if="${employee.dateOfTermination.after(#dates.createNow())}">Case 2</span>

或许,如果你喜欢这个开关:

<div th:switch="${employee.dateOfTermination.before(#dates.createNow())}">
    <span th:case="true">Case 1</span>
    <span th:case="false">Case 2</span>
</div>