百里香+春季日期转换 [英] thymeleaf +spring date converting

查看:30
本文介绍了百里香+春季日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据模型.我想使用这里的日期.

我在我的 html 中这样做:

<tr th:each="评论:${commentsInTask}"><tr th:each="comment : ${Comments}"><td th:text="${comment.user}">用户 ...</td><td th:text="${comment.createdAt}">日期...</td></tr></tr></tbody>

但它带来了:

<td>杰克</td><td>1.476787930289E9</td></tr></tr></tbody>

这部分是unix时间日期:1.476787930289E9

但是在我开始发布的图片中,您看到了.时间不是那样.

这是在域中

 public String getCreatedAtString() {返回 createdAtString;}public TaskComment setCreatedAtString(String createdAtString) {this.createdAtString = createdAtString;返回这个;}private ZonedDateTime createdAt = ZonedDateTime.now();

为什么我看不到开头图片中的日期格式?

解决方案

使用 Thymeleaf 格式:

<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td>

您将获得以下格式的输出:18-Oct-2016 14:44:05.

#dates:java.util.Date 对象的方法:格式化、组件提取等

<小时>

要将您的 createdAt 字段转换为 java.util.Date,请输入:

Date date = Date.from(java.time.ZonedDateTime.now().toInstant());

或者只是使用 java.util.Date 类型:

private Date createdAt = new Date();

这会将 cheatedAt 设置为当前日期.

<小时>

你也可以添加

然后你可以使用带有指定模式的ZoneDateTime:

${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')}

Thymeleaf - Java 8 Time 模块中查看更多信息API 兼容性.

This is my data model. I want to use date from here.

I do this in my 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>

but it brings:

<table>

    <tbody>
         <td>JACK</td>

        <td>1.476787930289E9</td>
    </tr>
    </tr>
    </tbody>
</table>

This part is unix timedate: 1.476787930289E9

but in the picture at beginning i posted, you saw. Timme is not that.

This is in domain

    public String getCreatedAtString() {
        return createdAtString;
    }

    public TaskComment setCreatedAtString(String createdAtString) {
        this.createdAtString = createdAtString;
        return this;
    }
private ZonedDateTime createdAt = ZonedDateTime.now();

Why cantt i see in date format which in picture at the beginning?

解决方案

Use Thymeleaf formatting:

<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td>

You'll get an output in the following format: 18-Oct-2016 14:44:05.

#dates: methods for java.util.Date objects: formatting, component extraction, etc.


To convert your createdAt field to java.util.Date type use:

Date date = Date.from(java.time.ZonedDateTime.now().toInstant());

Or just use java.util.Date type:

private Date createdAt = new Date();

this will set cheatedAt to current date.


Also you can add thymeleaf-extras-java8time dependency to your project to work with your ZonedDateTime type.

This module adds a #temporals object similar to the #dates or #calendars ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates:

Then you can use ZoneDateTime with the specified pattern:

${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')}

See more at Thymeleaf - Module for Java 8 Time API compatibility.

这篇关于百里香+春季日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆