Table of Contents
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 andMMM
as pattern. - Parse given monthName using DateTimeFormatter’s
parse()
method and get TemporalAccessor object. - Get month number using
get(ChronoField.MONTH_OF_YEAR)
method ofTemporalAccessor
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 |
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.
1 2 3 |
return cal.get(Calendar.MONTH) + 1; |
Further reading:
Using Month enum [ Java 8]
If you are interest only in English locale, you can use Month ENUM to get month number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 3 |
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.