I need to format an incomplete date only to the nearest month. My first assumption was to apply the MMMM yyyy template to it. This is great for many locales (English, German, ...), but gives an ugly result for languages ​​with flex, for example. Russian. I read the SimpleDateFormat documentation and found that "L" should indicate the name of the month, taking into account the insensitivity to the context: only what I need, since there is no day of the month in the template. So I tried the "LLLL yyyy" template. It works great for Russian and other bending languages, but instead does not work, for example. 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 his conclusion:
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 correctly only with "M", "hu" works great for both "M" and "L", "this is probably better with" L '(I don’t know , how important is the capital letter), while "ru", "fi" and "pl" give the correct result only with "L" (I really can only say this for Russian and Polish, but I suppose, similarly in Finnish language).
Questions:
Can I do something to make SimpleDateFormat or a similar date formatting class handle “L” correctly for all locales?
Alternatively, is there an easy way to determine whether to use "M" or "L" for any given language?
source share