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

Tho*_*ley 5 java jsf datetime

我有以下JSF代码来显示使用某种模式的日期.

<f:convertDateTime pattern="E, d MMM, yyyy" timeZone="#{localeBean.timeZone}" />
Run Code Online (Sandbox Code Playgroud)

我想通过localeBean将模式传递给它.有没有办法根据区域设置确定特定模式?

public LocaleBean() {
  this.defaultTimeZone = TimeZone.getDefault();
  this.strLocale = Locale.getDefault().toString();
  this.timeZone = defaultTimeZone.getDisplayName();
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

f:convertDateTime提供的type,dateStyletimeStyle这是依赖于的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>
Run Code Online (Sandbox Code Playgroud)

以下是英语语言环境的呈现方式:

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

等等..


dog*_*ane 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());
Run Code Online (Sandbox Code Playgroud)

印刷:

dd/MM/yy
M/d/yy
Run Code Online (Sandbox Code Playgroud)