2011-01-25 73 views
4

我有以下JSF代码来显示使用特定模式的日期。根据区域确定日期时间模式

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

我想通过localeBean将此模式传递给它。 有什么方法可以根据语言环境来确定特定模式吗?

回答

1

您可以试试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 
+0

我如何使用这还包括与AM或PM指示体24小时格式的时间? M/d/yy HH:mm AM/PM? – 2011-01-25 12:01:24

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 

等。