Java从整数中获取月份字符串

时间:2011-04-27 03:54:24

标签: java date

有没有更好的方法来压缩这种方法,即通过避免切换情况来降低圈复杂度?

String monthString;
        switch (month) {
            case 1:  monthString = "January";       break;
            case 2:  monthString = "February";      break;
            case 3:  monthString = "March";         break;
            case 4:  monthString = "April";         break;
            case 5:  monthString = "May";           break;
            case 6:  monthString = "June";          break;
            case 7:  monthString = "July";          break;
            case 8:  monthString = "August";        break;
            case 9:  monthString = "September";     break;
            case 10: monthString = "October";       break;
            case 11: monthString = "November";      break;
            case 12: monthString = "December";      break;
            default: monthString = "Invalid month"; break;
        }
        System.out.println(monthString);

7 个答案:

答案 0 :(得分:98)

尝试:

import java.text.DateFormatSymbols;
monthString = new DateFormatSymbols().getMonths()[month-1];

或者,您可以使用SimpleDateFormat:

import java.text.SimpleDateFormat;
System.out.println(new SimpleDateFormat("MMMM").format(date));

(您必须在Date对象中输入您的月份日期才能使用第二个选项。

答案 1 :(得分:16)

Month枚举

您可以使用Month枚举。此枚举被定义为Java 8及更高版本中内置的新java.time框架的一部分。

int monthNumber = 10;
Month.of(monthNumber).name();

输出结果为:

  

OCTOBER

本地化

通过在同一个枚举上调用getDisplayName来本地化为超出英语的语言。

String output = Month.OCTOBER.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH );

输出:

  

OCTOBRE

答案 2 :(得分:8)

获取包含月份名称的数组。

String[] str = {"January",      
   "February",
   "March",        
   "April",        
   "May",          
   "June",         
   "July",         
   "August",       
   "September",    
   "October",      
   "November",     
   "December"};

然后你想要用一个月的时间,如下:

if(i<str.length)
    monthString = str[i-1];
else
    monthString = "Invalid month";

答案 3 :(得分:3)

您可以拥有一个字符串数组并按索引访问。

  String months[] = {"January", "February", "March", "April",
                     "May", "June", "July", "August", "September",
                     "October", "November", "December"};

答案 4 :(得分:2)

这已经提到了,但是这里是一种将代码放在方法中的方法:

    public static String getMonthName(int monthIndex) {
         return new DateFormatSymbols().getMonths()[monthIndex].toString();
    }

或者如果你想创建一个比ArrayIndexOutOfBoundsException更好的错误:

    public static String getMonthName(int monthIndex) {
        //since this is zero based, 11 = December
        if (monthIndex < 0 || monthIndex > 11 ) {
            throw new IllegalArgumentException(monthIndex + " is not a valid month index.");
        }
        return new DateFormatSymbols().getMonths()[monthIndex].toString();
    }

答案 5 :(得分:0)

+---------+--------------+------------+--------+--------------+
| vehicle | Station_From | Station_To | pieces | Distance_all |
+---------+--------------+------------+--------+--------------+
|       1 | A            | B          |     10 |           50 |
|       1 | E            | E          |     10 |            5 |
|       1 | C            | D          |     11 |           63 |
+---------+--------------+------------+--------+--------------+

答案 6 :(得分:0)

DateFormatSymbols 类提供了易于使用的方法。

获取短月份字符串。例如:“ Jan”,“ Feb”等。

getShortMonths()

获取月份字符串。例如:“一月”,“二月”等。

getMonths()

示例代码以mmm格式返回月份字符串,

private static String getShortMonthFromNumber(int month){
    if(month<0 || month>11){
        return "";
    }
    return new DateFormatSymbols().getShortMonths()[month];
}