根据区域设置确定日期时间模式

时间:2011-01-25 10:29:44

标签: 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();
}

2 个答案:

答案 0 :(得分:6)

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

等。

答案 1 :(得分:2)

您可以尝试DateFormat.getDateInstance。例如:

   SimpleDateFormat f = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);
   System.out.println(f.toPattern());

   f = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
   System.out.println(f.toPattern());

打印:

dd/MM/yy
M/d/yy
相关问题