107

我得到一个整数,我需要在不同的语言环境中转换为月份名称:

地区 en-us 示例:
1 -> 一月
2 -> 二月

区域设置 es-mx 的示例:
1 -> Enero
2 -> Febrero

4

13 回答 13

227
import java.text.DateFormatSymbols;
public String getMonth(int month) {
    return new DateFormatSymbols().getMonths()[month-1];
}
于 2009-06-24T14:02:32.393 回答
34

tl;博士

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

由于 Java 1.8(或带有ThreeTen-Backport 的1.7 和 1.6 ),您可以使用它:

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

请注意,它integerMonth是从 1 开始的,即 1 代表一月。1 月至 12 月的范围始终为 1 到 12(即仅公历)。

于 2015-01-13T07:38:53.020 回答
34

您需要将 LLLL 用于独立的月份名称。这记录在SimpleDateFormat文档中,例如:

SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
于 2013-01-30T12:14:54.043 回答
16

我会使用 SimpleDateFormat。如果有更简单的方法来制作月历,有人纠正我,我现在用代码做这个,我不太确定。

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());
}
于 2009-06-24T14:09:07.623 回答
14

这就是我将如何做到的。我将把范围检查留给int month你。

import java.text.DateFormatSymbols;

public String formatMonth(int month, Locale locale) {
    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    String[] monthNames = symbols.getMonths();
    return monthNames[month - 1];
}
于 2009-06-24T14:12:28.257 回答
12

使用 SimpleDateFormat。

import java.text.SimpleDateFormat;

public String formatMonth(String month) {
    SimpleDateFormat monthParse = new SimpleDateFormat("MM");
    SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM");
    return monthDisplay.format(monthParse.parse(month));
}


formatMonth("2"); 

结果:二月

于 2011-09-28T02:02:41.333 回答
8

显然,在 Android 2.2 中,SimpleDateFormat 存在一个错误。

为了使用月份名称,您必须自己在资源中定义它们:

<string-array name="month_names">
    <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>

然后在您的代码中使用它们,如下所示:

/**
 * Get the month name of a Date. e.g. January for the Date 2011-01-01
 * 
 * @param date
 * @return e.g. "January"
 */
public static String getMonthName(Context context, Date date) {

    /*
     * Android 2.2 has a bug in SimpleDateFormat. Can't use "MMMM" for
     * getting the Month name for the given Locale. Thus relying on own
     * values from string resources
     */

    String result = "";

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH);

    try {
        result = context.getResources().getStringArray(R.array.month_names)[month];
    } catch (ArrayIndexOutOfBoundsException e) {
        result = Integer.toString(month);
    }

    return result;
}
于 2011-10-09T12:15:29.730 回答
6
于 2016-08-30T06:58:22.027 回答
1

当您将 DateFormatSymbols 类用于其 getMonthName 方法以按名称获取月份时,它会在某些 Android 设备中按数字显示月份。我通过这种方式解决了这个问题:

在 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>

在 Java 类中,只需像这样调用这个数组:

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);

            }

快乐编码:)

于 2015-03-09T08:57:14.597 回答
1

Kotlin 扩展

fun Int.toMonthName(): String {
    return DateFormatSymbols().months[this]
}

用法

calendar.get(Calendar.MONTH).toMonthName()
于 2019-08-26T10:27:28.167 回答
0

只需插入行

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

会成功的。

于 2019-02-22T19:25:40.940 回答
0
    public static void main(String[] args) {
    SimpleDateFormat format = new SimpleDateFormat("MMMMM", new Locale("en", "US"));
    System.out.println(format.format(new Date()));
}
于 2016-06-07T20:52:53.980 回答
0

尝试以一种非常简单的方式使用它并像您自己的函数一样调用它

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;
     }
于 2019-05-17T14:34:39.947 回答