Search code examples
javalocale

Java Locale and date format


I just don't get it. There is a site that supports only English and German languages. And there is an option for a user to specify his country so that dates, numbers and other locale-specific data would appear in an appropriate format.

The Locale is build as Locale locale = new Locale(lang, country). The date is being formatted as follows:

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
System.out.println("Date " + df.format(calendar.getTime()));

Today is 26 Nov 2013, and here is what people see depending on their country and chosen language:

  1. 11/26/13 sees a person from US looking at English page, locale en_US, ok.
  2. 26/11/13 sees a person from UK looking at English page, locale en_GB, ok.
  3. 11/26/13 sees a person from France looking at English page, locale en_FR, wrong.

The latter should see 26/11/13 (French people here agree with me) as the date format is country-specific, isn't it? What am I missing here?

Update The question is simple: why Locale("en", "FR") produces wrong date format?

Update 2 I'd appreciate if someone could explain (as a pat of an answer) why Java does not take into consideration the country when it comes to date format, which (in my POV) is purely country specific.


Solution

  • English is not official language in France, so there's no specific settings in any JVM for the English language in France. In result your locale changes back to English (en which default implementation is en_US). Here you can find full explanation.

    Here you can find all locales supported in JDK 7 and JRE 7 and there's no en_FR one.

    Update 2 I'd appreciate if someone could explain (as a pat of an answer) why Java does not take into consideration the country when it comes to date format, which (in my POV) is purely country specific.

    Date format may be country-specific, but you're creating Locale object which is predefined (not created dynamically). If JVM did not implement locale en_FR then it cannot return Locale which is mix of partialy-English-partialy-French. It just returns some specified object - the closest one is en. It's just how it's implemented. If it returned fr one then user could feel little uncomfortable since he wanted to display page in some kind of English dialect.