How can I convert an Integer to localized month name in Java?

后端 未结 13 2042
眼角桃花
眼角桃花 2020-11-27 03:20

I get an integer and I need to convert to a month names in various locales:

Example for locale en-us:
1 -> January
2 -> February

Example for locale e

相关标签:
13条回答
  • 2020-11-27 03:44

    tl;dr

    Month                             // Enum class, predefining and naming a dozen objects, one for each month of the year. 
    .of( 12 )                         // Retrieving one of the enum objects by number, 1-12. 
    .getDisplayName(
        TextStyle.FULL_STANDALONE , 
        Locale.CANADA_FRENCH          // Locale determines the human language and cultural norms used in localizing. 
    )
    

    java.time

    Since Java 1.8 (or 1.7 & 1.6 with the ThreeTen-Backport) you can use this:

    Month.of(integerMonth).getDisplayName(TextStyle.FULL_STANDALONE, locale);
    

    Note that integerMonth is 1-based, i.e. 1 is for January. Range is always from 1 to 12 for January-December (i.e. Gregorian calendar only).

    0 讨论(0)
  • 2020-11-27 03:44
        public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("MMMMM", new Locale("en", "US"));
        System.out.println(format.format(new Date()));
    }
    
    0 讨论(0)
  • 2020-11-27 03:44

    Try to use this a very simple way and call it like your own func

    public static String convertnumtocharmonths(int m){
             String charname=null;
             if(m==1){
                 charname="Jan";
             }
             if(m==2){
                 charname="Fev";
             }
             if(m==3){
                 charname="Mar";
             }
             if(m==4){
                 charname="Avr";
             }
             if(m==5){
                 charname="Mai";
             }
             if(m==6){
                 charname="Jun";
             }
             if(m==7){
                 charname="Jul";
             }
             if(m==8){
                 charname="Aou";
             }
             if(m==9){
                 charname="Sep";
             }
             if(m==10){
                 charname="Oct";
             }
             if(m==11){
                 charname="Nov";
             }
             if(m==12){
                 charname="Dec";
             }
             return charname;
         }
    
    0 讨论(0)
  • 2020-11-27 03:46

    I would use SimpleDateFormat. Someone correct me if there is an easier way to make a monthed calendar though, I do this in code now and I'm not so sure.

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    
    public String formatMonth(int month, Locale locale) {
        DateFormat formatter = new SimpleDateFormat("MMMM", locale);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, month-1);
        return formatter.format(calendar.getTime());
    }
    
    0 讨论(0)
  • 2020-11-27 03:47

    Just inserting the line

    DateFormatSymbols.getInstance().getMonths()[view.getMonth()] 
    

    will do the trick.

    0 讨论(0)
  • 2020-11-27 03:49

    There is an issue when you use DateFormatSymbols class for its getMonthName method to get Month by Name it show Month by Number in some Android devices. I have resolved this issue by doing this way:

    In String_array.xml

    <string-array name="year_month_name">
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>May</item>
        <item>June</item>
        <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>
        </string-array>
    

    In Java class just call this array like this way:

    public String[] getYearMonthName() {
            return getResources().getStringArray(R.array.year_month_names);
            //or like 
           //return cntx.getResources().getStringArray(R.array.month_names);
        } 
    
          String[] months = getYearMonthName(); 
               if (i < months.length) {
                monthShow.setMonthName(months[i] + " " + year);
    
                }
    

    Happy Coding :)

    0 讨论(0)
提交回复
热议问题