java - 将长值设为日期并在 Thymeleaf 中格式化

标签 java date thymeleaf

我的 Thymeleaf 模板中有这一行。

<td th:text="${activity.loggedTimestamp}"></td>

我想在格式化之前将这个长值转换为日期。如果它已经是一个日期,我会这样做:

<td th:text="${#dates.format(activity.loggedTimestamp)}"></td>

但是,由于它是一个很长的值,我想先把它设为一个日期。

在文档中找到了一些相关的方法。但是,不符合我的要求。

/*
 * Create date (java.util.Date) objects from its components
 */
${#dates.create(year,month,day)}
${#dates.create(year,month,day,hour,minute)}
${#dates.create(year,month,day,hour,minute,second)}
${#dates.create(year,month,day,hour,minute,second,millisecond)}

/*
 * Create a date (java.util.Date) object for the current date and time
 */
${#dates.createNow()}

/*
 * Create a date (java.util.Date) object for the current date (time set to 00:00)
 */
${#dates.createToday()}

最佳答案

引用 ThymeLeaf documentation :

We already mentioned that ${...} expressions are in fact OGNL (Object-Graph Navigation Language) expressions executed on the map of variables contained in the context.

For detailed info about OGNL syntax and features, you should read the OGNL Language Guide at: http://commons.apache.org/ognl/

所以您点击该链接到 OGNL documentation :

You can create new objects as in Java, with the new operator. One difference is that you must specify the fully qualified class name for classes other than those in the java.lang package.

This is only true with the default ClassResolver in place. With a custom class resolver packages can be mapped in such a way that more Java-like references to classes can be made. Refer to the OGNL Developer's Guide for details on using ClassResolver class (for example, new java.util.ArrayList(), rather than simply new ArrayList()).

OGNL chooses the right constructor to call using the same procedure it uses for overloaded method calls.

结论

你写:

<td th:text="${#dates.format(new java.util.Date(activity.loggedTimestamp))}"></td>

关于java - 将长值设为日期并在 Thymeleaf 中格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944180/

相关文章:

templates - thymeleaf :text - Put a text without removing HTML structures

java - :remove is not working 内的 hasRole()

java - 如何在 Spring Boot 中手动配置 JdbcTemplate?

javascript - 在javascript中将朱利安日期数字转换为正常日期(utc)

PHP:按今天/昨天制作过滤器

java - 显示 jTable 中两个给定日期之间的元素

java - Thymeleaf 中的嵌套循环绑定(bind)

java - 无法找到类型为 'org.hibernate.SessionFactory' 的 bean

要动画化的 Java TrayIcon 图像

java - 如何确定给定程序中符合垃圾收集条件的对象?