Java 获取 Thymeleaf 中的当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28738506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current date in Thymeleaf
提问by Fred K
How can I print current date (and time, eventually) from Thymeleaf? I'm trying these functions: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#datesbut I can't get them work.
如何从 Thymeleaf 打印当前日期(和时间,最终)?我正在尝试这些功能:http: //www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#dates但我无法让它们工作。
采纳答案by Andrea
Try with this:
试试这个:
${#dates.format(#dates.createNow(), 'dd MMM yyyy HH:mm')}
will be created a java.util.Date()
object then formatted as you prefer.
将创建一个java.util.Date()
对象,然后根据您的喜好进行格式化。
Using the #calendars
utility object
使用#calendars
实用程序对象
This is an alternative method:
这是另一种方法:
${#calendars.format(#calendars.createNow(), 'dd MMM yyyy HH:mm')}
the result will be the same.
结果是一样的。
回答by Mykola Lysenko
This one works fine for me:
这个对我来说很好用:
${#dates.format(#dates.createNow(),'YYYY/MM/dd HH:mm')}
${#dates.format(#dates.createNow(),'YYYY/MM/dd HH:mm')}
回答by Lucky
Just another way to get the current date and time in thymeleaf is using,
使用 thymeleaf 获取当前日期和时间的另一种方法是,
${execInfo.now}
The current date and time (
${execInfo.now}
), a Calendar object corresponding to the moment the template engine started its execution for this template.
当前日期和时间 (
${execInfo.now}
),一个 Calendar 对象,对应于模板引擎开始执行此模板的时刻。
You can create an WebContext to modify the context variables,
您可以创建一个 WebContext 来修改上下文变量,
WebContext ctx = new WebContext(request, servletContext, request.getLocale());
When the context is created, it creates a object which holds the two values for the template engine. The object name is execInfo
. The two variables are templateName
and now
. These variables can be accessed across anywhere in the templates.
创建上下文时,它会创建一个对象,该对象包含模板引擎的两个值。对象名称是execInfo
。这两个变量是templateName
和now
。可以在模板中的任何位置访问这些变量。
If you need to format the date format you can do like this,
如果您需要格式化日期格式,您可以这样做,
WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", dateFormat.format(cal.getTime()));
Example:
例子:
Current time : <div th:text="${execInfo.now.time}">Wed Feb 10 13:55:58 IST 2016</div>
回答by Sonu patel
I hope this works :
我希望这有效:
<b th:text="${#execInfo.now.time}"></b>
回答by nazar_art
The Date
is going to be processed by the #dates
utility.
The new LocalDateTime
, LocalDate
classes are going to be processed by the #temporals
utility.
在Date
将要被处理的#dates
实用程序。
新的LocalDateTime
,LocalDate
类将由#temporals
实用程序处理。
Set the format:
设置格式:
<p th:text="${#dates.format(standardDate, 'dd-MM-yyyy HH:mm')}"></p>
<p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy HH:mm')}"></p>
<p th:text="${#temporals.format(localDate, 'MM-yyyy')}"></p>
Where you have to sent it to a viewas following:
您必须将其发送到视图的位置,如下所示:
model.addAttribute("standardDate", new Date());
model.addAttribute("localDateTime", LocalDateTime.now());
model.addAttribute("localDate", LocalDate.now());
Formatting the LocalDate
is only possible if we will specify only the particular date fields, skipping the time fields.
LocalDate
只有当我们只指定特定的日期字段,跳过时间字段时,才有可能格式化。
Output result:
输出结果:
24-05-2019 21:57
24-05-2019 21:57
24-05-2019