我的 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()}
最佳答案
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 simplynew 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/