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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:43:08  来源:igfitidea点击:

Get current date in Thymeleaf

javaspringdatethymeleaf

提问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 #calendarsutility 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 templateNameand now. These variables can be accessed across anywhere in the templates.

创建上下文时,它会创建一个对象,该对象包含模板引擎的两个值。对象名称是execInfo。这两个变量是templateNamenow。可以在模板中的任何位置访问这些变量。

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 Dateis going to be processed by the #datesutility.
The new LocalDateTime, LocalDateclasses are going to be processed by the #temporalsutility.

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 LocalDateis 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