这是我的数据模型.我想从这里使用日期.
我在我的HTML中这样做:
<table th:if="${!commentsInTask.empty}">
<tbody>
<tr th:each="Comments : ${commentsInTask}">
<tr th:each="comment : ${Comments}">
<td th:text="${comment.user}">user ...</td>
<td th:text="${comment.createdAt}">date ...</td>
</tr>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
但它带来:
<table>
<tbody>
<td>JACK</td>
<td>1.476787930289E9</td>
</tr>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
这部分是unix时间:1.476787930289E9
但是在我发布的图片中,你看到了.蒂姆不是那个.
这是在域中
public String getCreatedAtString() {
return createdAtString;
}
public TaskComment setCreatedAtString(String createdAtString) {
this.createdAtString = createdAtString;
return this;
}
private ZonedDateTime createdAt = ZonedDateTime.now();
Run Code Online (Sandbox Code Playgroud)
为什么我会在日期格式中看到开头的图片?
使用Thymeleaf格式:
<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td>
Run Code Online (Sandbox Code Playgroud)
您将获得以下格式的输出:18-Oct-2016 14:44:05.
#dates:java.util.Date对象的方法:格式化,组件提取等.
要将createdAt字段转换为java.util.Date类型,请使用:
Date date = Date.from(java.time.ZonedDateTime.now().toInstant());
Run Code Online (Sandbox Code Playgroud)
或者只使用java.util.Date类型:
private Date createdAt = new Date();
Run Code Online (Sandbox Code Playgroud)
这将设置cheatedAt为当前日期.
您还可以将thymeleaf-extras-java8time依赖项添加到项目中以使用您的ZonedDateTime类型.
此模块添加#temporals类似于标准方言中的一个#dates或多个对象#calendars,允许从Thymeleaf模板格式化和创建临时对象:
然后你可以使用ZoneDateTime指定的模式:
${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅Thymeleaf - Module for Java 8 Time API兼容性.
| 归档时间: |
|
| 查看次数: |
6467 次 |
| 最近记录: |