I need to format incomplete date only to month precision. My first guess was to apply pattern "MMMM yyyy" to it. This works fine for many locales (English, German, ...), but gives ugly result for languages with flexion, e.g. Russian. I read documentation of SimpleDateFormat
and found that 'L' is supposed to give context-insensitive month name: just what I need, as there is no day of month in the pattern. So I tried pattern "LLLL yyyy" instead. It works perfectly for Russian and other flexion languages, but instead fails for e.g. English and German...
Here is the test code:
import java.text.*;
import java.util.*;
public class test
{
public static void main (String[] arguments) throws Exception
{
for (String locale : new String[] { "en", "de", "hu", "it", "ru", "fi", "pl" })
System.out.format ("%s: %s \t%s\n", locale,
new SimpleDateFormat ("MMMM yyyy", new Locale (locale)).format (new Date ()),
new SimpleDateFormat ("LLLL yyyy", new Locale (locale)).format (new Date ()));
}
}
And its output:
en: September 2015 0009 2015
de: September 2015 0009 2015
hu: szeptember 2015 szeptember 2015
it: settembre 2015 Settembre 2015
ru: сентября 2015 Сентябрь 2015
fi: syyskuuta 2015 syyskuu 2015
pl: września 2015 wrzesień 2015
So, for the tested locales: 'en' and 'de' work properly only with 'M', 'hu' is fine with both 'M' and 'L', 'it' is probably better off with 'L' (don't know how important capital letter is here), while 'ru', 'fi' and 'pl' give correct output only with 'L' (this I can really tell only for Russian and Polish, but I presume it's similar in Finnish).
Questions:
Can I do something to make
SimpleDateFormat
or a similar date formatting class treat 'L' properly for all locales?Alternatively, is there a simple way to tell if 'M' or 'L' should be used for any given locale?