我有以下 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
为此提供了 type
、dateStyle
和 timeStyle
属性,这取决于 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/