春季启动百里香叶格式日期给出"Cannot retrieve hour from null"



我有一个弹簧启动应用程序,我试图以格式化的方式显示出生日期。如果我不使用百里叶日期格式选项,则可以显示日期,但是如果使用格式选项,则会给我"Cannot apply format on null"

 <td th:text="*{dateOfBirth}">06/23/2013</td>
 output: 1990-01-21 00:03:00.0
 <td th:text="${#dates.format(dateOfBirth,'dd-mm-yyyy')}">06/23/2013</td>
 output ::Cannot apply format on null

我的pogo类中的dateofthert字段如下。

@JsonDeserialize(using = CustomeDateDeserializer.class)
@DateTimeFormat(pattern="dd-mm-yyyy")
private Date dateOfBirth;

我尝试使用差异组合,但都给我带来了相同的错误。任何人都可以帮助我在这里缺少的东西。

*{dateOfBirth}具有特定的含义。当您使用*符号时,您指的是您选择的th:object的属性。当您使用${}表达式时,您会失去它。为了指定相同的变量,您应该使用#object表达式变量(或将完整的路径命名为所讨论的变量(。

<td th:text="${#dates.format(#object.dateOfBirth,'dd-MM-yyyy')}">06/23/2013</td>

注意:由于您已经指定了变量的日期格式,因此我认为双支式语法应该适合您:

<td th:text="${{#object.dateOfBirth}}">06/23/2013</td>

相关内容

最新更新