在Java中将Month String转换为Integer

时间:2010-02-15 21:16:30

标签: java parsing datetime

给出一个月份字符串,例如:

    "Feb"
or
    "February"

是否有任何核心Java或第三方库功能允许您以区域设置无关的方式将此字符串转换为相应的月份数?

6 个答案:

答案 0 :(得分:44)

您可以使用SimpleDateFormat解析月份:

    Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse("Feb");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH);
    System.out.println(month == Calendar.FEBRUARY);

小心比较int month和int(它等于2!)。最安全的方法是使用Calendar的静态字段(例如Calendar.FEBRUARY)来比较它们。

答案 1 :(得分:27)

使用Joda时间替代SimpleDateFormat:

    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    ...

    // if default locale is ok simply omit '.withLocale(...)'
    DateTimeFormatter format = DateTimeFormat.forPattern("MMM");
    DateTime instance        = format.withLocale(Locale.FRENCH).parseDateTime("août");  

    int month_number         = instance.getMonthOfYear();
    String month_text        = instance.monthOfYear().getAsText(Locale.ENGLISH);

    System.out.println( "Month Number: " + month_number );
    System.out.println( "Month Text:   " + month_text   );

    OUTPUT:
        Month Number: 8
        Month Text:   August

答案 2 :(得分:5)

Java 8解决方案:

DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMM")
                                            .withLocale(Locale.ENGLISH);
TemporalAccessor accessor = parser.parse("Feb");
System.out.println(accessor.get(ChronoField.MONTH_OF_YEAR));  // prints 2

答案 3 :(得分:3)

答案 4 :(得分:1)

你可以设置一个开关声明,如下所示(下图)。 我发布这个以防万一有人想要一个简单易懂的解决方案 我知道在输入之前我会想要它:

switch(input2) {
            case "january":
            case "jan":
                input2 = "1";
            break;

            case "febuary":
            case "feb":
                input2 = "2";
            break;

            case "march":
            case "mar":
                input2 = "3";
            break;

            case "april":
            case "apr":
                input2 = "4";
            break;

            case "may":
                input2 = "5";
            break;

            case "june":
            case "jun":
                input2 = "6";
            break;

            case "july":
            case "jul":
                input2 = "7";
            break;

            case "august":
            case "aug":
                input2 = "8";
            break;

            case "september":
            case "sep":
            case "sept":
                input2 = "9";
            break;

            case "october":
            case "oct":
                input2 = "10";
            break;

            case "november":
            case "nov":
                input2 = "11";
            break;

            case "december":
            case "dec":
                input2 = "12";
            break;
            }

答案 5 :(得分:0)

方法如下:

 String a = "August 10, 2021 10:00 AM";

    SimpleDateFormat fromUser = new SimpleDateFormat("MMMM dd, yyyy hh:mm a");

    SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy");

    String reformattedStr = myFormat.format(fromUser.parse(a));

    System.out.println(reformattedStr); // 08/10/2021