Java Spring + Thymeleaf:用户所在时区的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22743346/
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
Spring + Thymeleaf: time in user's timezone
提问by igo
How can I print date and time is specified timezone with Thymeleaf? Something like:
如何使用 Thymeleaf 打印指定时区的日期和时间?就像是:
<span th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm', 'PST')}">2010-01-01 16:30</span>
采纳答案by Blejzer
As I was puzzled by this question, I searched extensively for possible solutions.
当我对这个问题感到困惑时,我广泛搜索了可能的解决方案。
These are my findings: I did not found any clean function for changing timezone and displaying it like it is in jsp:
这些是我的发现:我没有找到任何用于更改时区并像在 jsp 中一样显示它的干净函数:
<fmt:timeZone value="US">
<fmt:formatDate value="${today}" type="both" />
</fmt:timeZone>
Possible solution, that works would be to create calendar instance using createForTimeZone and format it, since it returns a raw calendar value, so from this:
可能的解决方案是使用 createForTimeZone 创建日历实例并对其进行格式化,因为它返回一个原始日历值,因此:
#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone)
you would get something like this:
你会得到这样的东西:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=14,WEEK_OF_MONTH=1,DAY_OF_MONTH=24,DAY_OF_YEAR=91,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=6,HOUR_OF_DAY=7,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]
As you can see (you have to look carefully) it did converted time to the timezone provided.
如您所见(您必须仔细查看)它确实将时间转换为提供的时区。
Now, I still haven't gotten to the point where I can get it all to work fine, but if you add calendars.format in front of this, you would get it to properly show time in the given timezone. ${#calendars.format(#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone), 'dd-MMM-yyyy HH:mm')}
现在,我还没有达到可以让一切正常工作的程度,但是如果你在这之前添加 calendars.format,你会得到它在给定的时区正确显示时间。 ${#calendars.format(#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone), 'dd-MMM-yyyy HH:mm')}
Adding "zzz" to the end of the string, always return my locale timezone. I guess there are way to work this out so it looks better, but main point for me was to find out if it was possible at all.
将“zzz”添加到字符串的末尾,始终返回我的语言环境时区。我想有办法解决这个问题,所以它看起来更好,但对我来说,重点是找出是否有可能。
Examples that work:
有效的例子:
${#dates.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'PST'), 'yyyy-MMM-dd HH:mm')}
${#calendars.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'CET'), 'yyyy-MMM-dd HH:mm')}
and either one would return identical results.
并且任何一个都会返回相同的结果。
Here are the results when comparing same format, using PST and CET:
以下是使用 PST 和 CET 比较相同格式时的结果:
2014-Feb-24 16:00
2014-Feb-24 07:00
or:
或者:
2014-Mar-01 03:00
2014-Feb-28 18:00
Regards,
问候,
回答by Marek R
Another approach to the same problem may be to use your own static methods:
解决同一问题的另一种方法可能是使用您自己的静态方法:
${T(xx.xxx.utils.DateUtils).format(myDate, 'yyyy-MM-dd HH:mm', 'CET')}
public static String format(Date date, String pattern, String timeZone) {
TimeZone tz = TimeZone.getTimeZone(timeZone);
return DateFormatUtils.format(date, pattern, tz);
}
or even directly from lang3 (does not work on GAE because of some class access restrictions in sun.util.calendar package):
甚至直接来自 lang3(由于 sun.util.calendar 包中的某些类访问限制,因此在 GAE 上不起作用):
<div th:with="timeZone=${T(java.util.TimeZone).getTimeZone('CET')}">
<span th:text="${T(org.apache.commons.lang3.time.DateFormatUtils).format(myDate, 'yyyy-MM-dd HH:mm', timeZone)}"></span>
</div>
回答by Martin Podval
I found this answer when I wanted to format LocalDateTime
to some time zone in the templates. It turned out that the purpose of LocalDateTime
is to do not work with time zones at all.
当我想LocalDateTime
在模板中格式化为某个时区时,我找到了这个答案。事实证明, 的目的LocalDateTime
是根本不使用时区。
However, there is also a class called ZonedDateTime
which purpose is obvious. You can also use LocalDateTime#atZone
which creates a new instance of local
converted to the new zone.
但是,还有一个类叫做ZonedDateTime
which 目的是显而易见的。您还可以使用LocalDateTime#atZone
which 创建local
转换到新区域的新实例。
Note that usual DateTimeFormatter
ignores any time zone settings in the case of local date time
but not in the case of zoned date time
. So you can use usual formatters as well in the templates.
请注意,DateTimeFormatter
在 的情况下,通常会忽略任何时区设置,local date time
但在 的情况下不会zoned date time
。因此,您也可以在模板中使用常用的格式化程序。
回答by Downhillski
The server renders the page based on the server time, in order to get the timezone of the user from the request, the user when submitting the request should attach the timezone information in the request headers or parameters, so that the server knows the appropriate time zone to render. To do that, use javascript to get the browser's time zone.
服务器根据服务器时间渲染页面,为了从请求中获取用户所在的时区,用户在提交请求时应在请求头或参数中附加时区信息,以便服务器知道合适的时间要渲染的区域。为此,请使用 javascript 获取浏览器的时区。
回答by Predrag Pramenko
Using Thymeleaf's #temporals
doesn't provide the ability to use the constructor new Temporals(locale, zoneId)
.
使用 Thymeleaf#temporals
不提供使用构造函数的能力new Temporals(locale, zoneId)
。
Create your own Temporals temporals = new Temporals(LocaleContextHolder.getLocale(), zone)
(somewhere, session bean, or so - zone
should probably come from your user's preferences), and put it available in the controller (eg. as "temporalsZone"
).
创建您自己的Temporals temporals = new Temporals(LocaleContextHolder.getLocale(), zone)
(某处,会话 bean 等 -zone
可能应该来自您用户的偏好),并将其放在控制器中(例如 as "temporalsZone"
)。
Then use it in the UI: th:text="${temporalsZone.format(value, pattern, #locale)}">
and enjoy the full support of #temporals
.
然后在 UI 中使用它:th:text="${temporalsZone.format(value, pattern, #locale)}">
并享受#temporals
.