Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine datetime pattern based on locale

Tags:

java

datetime

jsf

I have the following JSF code to display a date using a certain pattern.

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

I would like to pass the pattern to this via the localeBean also. Is there any way to determine the specific pattern based on the locale?

public LocaleBean() {
  this.defaultTimeZone = TimeZone.getDefault();
  this.strLocale = Locale.getDefault().toString();
  this.timeZone = defaultTimeZone.getDisplayName();
}
like image 782
Thomas Buckley Avatar asked Jan 25 '11 10:01

Thomas Buckley


1 Answers

The f:convertDateTime provides the type, dateStyle and timeStyle attributes for this which is dependent on the viewroot's locale.

Assuming 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>

Here's how it renders like with English locale:

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

German:

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

Dutch:

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

French:

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

Etc..

like image 184
BalusC Avatar answered Sep 30 '22 15:09

BalusC