JSTL formatDate ignoring locale

匆匆过客 提交于 2020-01-24 08:01:47

问题


I want to localize dates with JSTL, and I am trying to do it like below.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:setLocale value="da_DK" scope="application" />
<fmt:formatDate value="${some.date}" dateStyle="FULL" />

some.date is a java.util.Date instance. I have played around with various different locales, but nothing is working. I get the following output no matter which locale I choose: Tuesday, January 13, 2015. I also tried the following, after removing the above call to setLocale():

<jsp:useBean id="now" class="java.util.Date" />

<fmt:setLocale value="en_US" />
<fmt:formatDate value="${now}" dateStyle="FULL" />

<fmt:setLocale value="fr_FR" />
<fmt:formatDate value="${now}" dateStyle="FULL" />

The above outputs Tuesday, January 13, 2015 two times, and the same happens for every locale that I tried. The only thing I have configured related to locale is the following (Spring MVC project):

@Bean
public CookieLocaleResolver localeResolver() {
    Locale locale = new Locale("dk");

    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(locale);

    return localeResolver;
}

Shouldn't the text be translated into the language defined by the locale, or the format changed at the very least? Any ideas what I am doing wrong here? I tried every example that I could find online, but the result is the same.


回答1:


I would just change the scope to session as below

<fmt:setLocale value="fr_FR" scope="session"/>

Date in France:
<fmt:formatDate value="${now}" dateStyle="full"/> <br/>

<fmt:setLocale value="en_US" scope="session"/>
 Date in US: 
<fmt:formatDate value="${now}" dateStyle="full" /> <br/>


来源:https://stackoverflow.com/questions/27925049/jstl-formatdate-ignoring-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!