java - 根据语言环境确定日期时间模式

标签 java datetime jsf

我有以下 JSF 代码来使用特定模式显示日期。

<f:convertDateTime pattern="E, d MMM, yyyy" timeZone="#{localeBean.timeZone}" />

我也想通过 localeBean 将模式传递给它。 有什么办法可以根据语言环境确定具体的模式吗?

public LocaleBean() {
  this.defaultTimeZone = TimeZone.getDefault();
  this.strLocale = Locale.getDefault().toString();
  this.timeZone = defaultTimeZone.getDisplayName();
}

最佳答案

f:convertDateTime为此提供了 typedateStyletimeStyle 属性,这取决于 viewroot 的语言环境。

假设 Facelets:

<!DOCTYPE html>
<html lang="#{localeBean.language}"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view locale="#{localeBean.locale}">
    <h:head>
        <title>SO question 4792373</title>
    </h:head>
    <h:body>
        <h:outputText value="#{bean.date}">
            <f:convertDateTime type="date" dateStyle="short" />
        </h:outputText>
        <br />
        <h:outputText value="#{bean.date}">
            <f:convertDateTime type="date" dateStyle="medium" />
        </h:outputText>
        <br />
        <h:outputText value="#{bean.date}">
            <f:convertDateTime type="date" dateStyle="long" />
        </h:outputText>
        <br />
        <h:outputText value="#{bean.date}">
            <f:convertDateTime type="date" dateStyle="full" />
        </h:outputText>
    </h:body>
</f:view>
</html>

这是它在英语语言环境中的呈现方式:

1/25/11
Jan 25, 2011
January 25, 2011
Tuesday, January 25, 2011

德语:

25.01.11
25.01.2011
25. Januar 2011
Dienstag, 25. Januar 2011

荷兰语:

25-1-11
25-jan-2011
25 januari 2011
dinsdag 25 januari 2011

法语:

25/01/11
25 janv. 2011
25 janvier 2011
mardi 25 janvier 2011

等等

关于java - 根据语言环境确定日期时间模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4792373/

相关文章:

python - 日期交叉点和空间可用性

html - html表格行的行计数器

ajax - 调用 <h :commandLink> action method using <f:ajax>

java - 如何在 j_security_check 之后重定向 2 个不同的页面

java - 如何在 eclipse 中为 android ndk c++ 制作 javadoc 样式注释?

java - 关于 EBO 如何工作的一些说明(元素数组缓冲区)

java - 部署在使用 JMS/AMQP 消息的 Cloud Foundry 上的应用程序

java - Action 标签在 Struts 2 中不执行

Java:如何获得下一次匹配模式

.net - DateTime.Now 抛出异常