Month name as a string

前端 未结 11 2183
悲哀的现实
悲哀的现实 2020-11-27 02:49

I\'m trying to return the name of the month as a String, for instance \"May\", \"September\", \"November\".

I tried:

int month = c.get(Calendar.MONTH         


        
11条回答
  •  情话喂你
    2020-11-27 03:45

    "MMMM" is definitely NOT the right solution (even if it works for many languages), use "LLLL" pattern with SimpleDateFormat

    The support for 'L' as ICU-compatible extension for stand-alone month names was added to Android platform on Jun. 2010.

    Even if in English there is no difference between the encoding by 'MMMM' and 'LLLL', your should think about other languages, too.

    E.g. this is what you get, if you use Calendar.getDisplayName or the "MMMM" pattern for January with the Russian Locale:

    января (which is correct for a complete date string: "10 января, 2014")

    but in case of a stand-alone month name you would expect:

    январь

    The right solution is:

     SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
     dateFormat.format( date );
    

    If you are interested in where all the translations come from - here is the reference to gregorian calendar translations (other calendars linked on top of the page).

提交回复
热议问题