java JSTL 和时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4102223/
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-10-30 04:46:15  来源:igfitidea点击:

JSTL and timezones

javajspjstl

提问by Roman Goyenko

I have some questions regarding timezones. We store all our date in UTC time, but we need to show some of them in local (US Eastern) time and UTC at the same time.

我有一些关于时区的问题。我们以 UTC 时间存储所有日期,但我们需要同时以本地(美国东部)时间和 UTC 时间显示其中一些日期。

Here is my test, I have a date in UTC and want to display it in UTC and local time:

这是我的测试,我有一个 UTC 日期,并希望以 UTC 和本地时间显示它:

<html>
<!-- let's assume this date is in UTC, I get it from Database in my code -->
<jsp:useBean id="dateValue" class="java.util.Date" />

GMT
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="GMT"/>
</html>
<!-- Displays the original time +4  - not what I need-->

No time zone
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z"/> 
<!-- Displays the original time, but timezone is EDT -->

US/Eastern
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="US/Eastern"/> 
<!-- Displays the original time, timezone is EDT, I need original + 4 -->

</html>

To reiterate: I have a UTC time from database and want to format it and show in UTC timezone. Server runs in other timezone than UTC.

重申:我有一个来自数据库的 UTC 时间,想要格式化它并以 UTC 时区显示。服务器在 UTC 以外的其他时区运行。

Basically I need a function like

基本上我需要一个像

convertToTimezone(date, originalTimeZone, desiredTimeZone). 

What fmt:formatDate provides is something like

fmt:formatDate 提供的内容类似于

convertToTimezone(date, serverTimeZone, desiredTimeZone). 

I could hack it, but that usually causes problems when there is a daylight time saving, etc.

我可以破解它,但是当有夏令时等时,这通常会导致问题。

P.S. For those people looking for answers - one thing to do is to run your server in UTC, then the conversions would work fine. If can't change that, the only way to go is to create the new date with UTC time zone, do it explicitly, I myself converted the time I had to text, then created a new date and provided the data from the text and UTC timezone. If you don't do that, the timezone is read from the server.

PS 对于那些寻找答案的人 - 要做的一件事是在 UTC 中运行您的服务器,然后转换会正常工作。如果不能改变,唯一的办法就是用 UTC 时区创建新日期,明确地做,我自己将时间转换为文本,然后创建一个新日期并提供文本中的数据和UTC 时区。如果您不这样做,则会从服务器读取时区。

回答by BalusC

I don't understand your problem. It's all working properly. You seem to be expecting that new Date()would treat the computer time as GMT, i.e. 19:21 GMT. This is wrong. It calculates the realGMT time based on the computer's timezone. The fmt:setTimeZonedoesn't change the computer's timezone and also not the outcome of new Date(). The fmt:setTimeZonejust sets the default timezone of all dates which are to be formatted by fmt:formatDate. This is only mandatory if the computer's timezone is not GMT.

我不明白你的问题。一切正常。您似乎期望new Date()将计算机时间视为 GMT,即 19:21 GMT。这是错误的。它根据计算机的时区计算实际GMT 时间。在fmt:setTimeZone不改变电脑的时区,也没有结果new Date()。在fmt:setTimeZone刚刚设置这些都是通过格式化所有日期的默认时区fmt:formatDate。只有当计算机的时区不是 GMT 时,这才是强制性的。

Remove the fmt:setTimeZoneand you'll see that the dates are (incorrectly) off by 4 hour. Then start the server with -Duser.timezone=UTC(to override computer's timezone being the default) and you'll see that it's correct again. Also give it a try with realdates from the DB instead of new Date().

删除fmt:setTimeZone,您会看到日期(错误地)偏离了 4 小时。然后启动服务器-Duser.timezone=UTC(以覆盖计算机的默认时区),您将再次看到它是正确的。还可以尝试使用来自数据库的真实日期而不是new Date().

Regardless, running the server in a timezone other than UTC is a bad practice. It would only lead to trouble in all colors. I strongly recommend to run the server in UTC. If you don't want to change the machine's timezone for some reason, then you can do that by adding -Duser.timezone=UTCto server's VM arguments. Use UTC all the timeand apply the timezone only at the moment you're displayingit to the enduser. See also DST and Timezone best practices.

无论如何,在 UTC 以外的时区运行服务器是一种不好的做法。它只会导致所有颜色的麻烦。我强烈建议在 UTC 中运行服务器。如果您出于某种原因不想更改机器的时区,那么您可以通过添加-Duser.timezone=UTC到服务器的 VM 参数来实现。始终使用 UTC并仅在您向最终用户显示时区时应用时区。另请参阅DST 和时区最佳实践

回答by Matt Ball

Java assumes that your time is GMT+4 because your system time is GMT+4.

Java 假设您的时间是 GMT+4,因为您的系统时间是 GMT+4。

回答by Sackers

Try:

尝试:

<fmt:setTimeZone value="Europe/London" scope="session"/>

回答by borchvm

You need to use the international time (UTC/Zulu) to add the following schedule client time use, for example "GMT+1". See this example.

您需要使用国际时间(UTC/Zulu)添加以下时间表客户端时间使用,例如“GMT+1”。请参阅此示例。

Put this parameter as argument in your server to set UTC time use, in this case is for tomcat:

将此参数作为参数放在您的服务器中以设置 UTC 时间使用,在本例中用于 tomcat:

-Duser.timezone="UTC"

-Duser.timezone="UTC"

/* Java */      

@RequestMapping(value = "/web", method = { RequestMethod.POST, RequestMethod.GET })
public String web(Model model, HttpSession session, Locale locale) {

    Date today = new Date();
    model.addAttribute("currentTime", today);
    model.addAttribute("timezone", "GMT+1");

    return "web";
}

To show date choose your pattern you want (properties)

要显示日期,请选择您想要的模式(属性)

/* JSP web */

<fmt:timeZone value="${timezone}">
<spring:message code="date_format_dateMin" var="pattern"/>
<fmt:formatDate value="${currentTime}" timeZone="${timezone}" pattern="${pattern}" var="searchFormated" />
<span class="innerLabel">${searchFormated}</span>   
</fmt:timeZone>

/* Properties */

date_format_dateMin=yyyy/MM/dd HH:mm
date_format=yyyy/MM/dd HH:mm:ss    
date_format2=yyyy/MM/dd
date_format3_js=yy/mm/dd
date_format4_time=HH:mm
date_format4=dd/MM/yyyy HH:mm:ss

回答by borchvm

To convert UTC time I use this function:

要转换 UTC 时间,我使用此函数:

 public Date getUTCDateFromString(String fecha, Locale yourLocale) {
    SimpleDateFormat simpleDateFormat = new 
    SimpleDateFormat("yyyy-MM-dd HH:mm:ss", yourLocale);
    TimeZone utcZone = TimeZone.getTimeZone("UTC");
    simpleDateFormat.setTimeZone(utcZone);
    Date date = null;
    try {
        date = simpleDateFormat.parse(fecha);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
    }

回答by Syed Priom

<c:when test="${currentField eq 'dateline'}">
  <c:set target="${storyContent}" property="publishedDate">
  <fmt:formatDate value="${article.publishedDateAsDate}" timeZone="Asia/Dacca"       pattern="${storyContent.dateFormat}"/>
  </c:set>

I have been messing with this problem for 3 days.

我已经被这个问题困扰了 3 天了。

First it was like this -

起初是这样的——

<fmt:formatDate value="${article.publishedDateAsDate}" pattern="${storyContent.dateFormat}"/>

which showed the date in server time UTC+00 hrs.

它以服务器时间 UTC+00 小时显示日期。

I wanted to show it in Dhaka time, keeping the server time unchanged. Adding this solved the problem-

我想在达卡时间展示它,保持服务器时间不变。添加这个解决了问题 -

timeZone="Asia/Dacca"

timeZone="亚洲/达卡"

You can find all the Java timezone details here http://joda-time.sourceforge.net/timezones.html

您可以在此处找到所有 Java 时区详细信息http://joda-time.sourceforge.net/timezones.html