1

I have a calendar where I can pick the date and time (only hours and minutes):

<p:calendar locale="de" pattern="dd.MM.yyyy HH:mm"

No I have to provide english translations as well. So I need to remove the locale and pattern part.

Is there a possiblility to have a locale aware calendar with date and time (no seconds) ?

Summary: When locale is German, I want to have dd.mm.yyyy HH:mm. When the locale is English, I want to have mm/dd/yyyy HH:mm

matthias
  • 1,938
  • 23
  • 51

1 Answers1

2

Hook the pattern attribute up to a bean method which returns the pattern you want for each given locale.

<p:calendar pattern="#{bean.localePattern}">

public String getLocalePattern() {
    Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
    if (Locale.GERMAN.equals(locale)) {
        return "dd.MM.yyyy HH:mm";
    } else if (Locale.ENGLISH.equals(locale)) {
        return "MM/dd/yyy HH:mm";
    }else {
        //return default pattern...
    }
}

Edit: Jaspers suggestion to take advantage of a localized resource bundle is a great one (if you're already/plan to use JSF's built in localization features).

Rung Ip
  • 81
  • 4
  • removing `:ss` from the pattern seems to be more dynamic to me. However, note that the month is represented by `M` (uppercase) – jp-jee Sep 20 '17 at 19:03