Convert Month Name to Number in Java

In this post, we will see how to convert Month name to number in Java.

Please note that we will use first three letter of month name (First letter as capital) to convert it to Number.

There are multiple ways to convert month name to number in java. Let’s go through.

Using DateTimeFormatter and TemporalAccessor

If you are using Java 8 and above, this is preferred solution.

  • Create DateTimeFormatter object with given local and MMM as pattern.
  • Parse given monthName using DateTimeFormatter’s parse() method and get TemporalAccessor object.
  • Get month number using get(ChronoField.MONTH_OF_YEAR) method of TemporalAccessor.
package org.arpit.java2blog;

import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

public class ConvertMonthNameToNumberJava8 {

    public static void main(String[] args) throws ParseException {
        int monthNumber1 = getNumberFromMonthName("Sep", Locale.ENGLISH);
        System.out.println("Month number of September in English: "+monthNumber1);

        int monthNumber2 = getNumberFromMonthName("févr.", Locale.FRENCH);
        System.out.println("Month number of Oktober  in German:"+monthNumber2);
    }

    public static int getNumberFromMonthName(String monthName, Locale locale) throws ParseException {

        DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("MMM")
                                        .withLocale(locale);
        TemporalAccessor temporalAccessor = dtFormatter.parse(monthName);
        return temporalAccessor.get(ChronoField.MONTH_OF_YEAR);
    }
}

Output

Month number of September in English: 9
Month number of February in French: 2

Using Calendar class with SimpleDateFormat

You can use SimpleDateFormat to convert MonthName to Date object and set Date to Calendar instance and use Calendar.get() method to get month number.

You can specify Locale as per your requirements.

Please note that Calendar class follows 0th based month index.
0 for January
1 for Febuary
…
…
11 for December

package org.arpit.java2blog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class ConvertMonthNameToNumberMain {

    public static void main(String[] args) throws ParseException {
        int monthNumber1 = getNumberFromMonthName("Sep", Locale.ENGLISH);
        System.out.println("Month number of September in English: "+monthNumber1);

        int monthNumber2 = getNumberFromMonthName("Okt", Locale.GERMAN);
        System.out.println("Month number of Oktober  in German:"+monthNumber2);
    }

    public static int getNumberFromMonthName(String monthName,Locale locale) throws ParseException {

        Date date = new SimpleDateFormat("MMM", locale).parse(monthName);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH);
    }
}

Output

Month number of September in English: 8
Month number of Oktober  in German:9

If you want to number from 1 to 12, you can simply add 1 at highlight line in above code.

 return cal.get(Calendar.MONTH) + 1;

Using Month enum [ Java 8]

If you are interest only in English locale, you can use Month ENUM to get month number.

package org.arpit.java2blog;

import java.time.Month;

public class ConvertMonthNameToNumberEnglish {

    public static void main(String[] args) {
        int monthNumber1 = getNumberFromMonthName("SEPTEMBER");
        System.out.println("Month number of September in English: "+monthNumber1);
    }

    public static int getNumberFromMonthName(String monthName) {
        return Month.valueOf(monthName.toUpperCase()).getValue();
    }
}

Output

Month number of September in English: 9

Please note that you need to pass complete month name such as SEPTEMBER for Month enum.

That’s all about how to convert Month name to number in Java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.