Convert between Month Name and Month Number in Java

In real-world applications, we can get information from several sources and in different formats, including date-time input data. This tutorial discusses one similar scenario where we get the Month information in either number or name, and we need to convert it into another.

For example, we get the month name as “Jan” and we need to convert it to Month number “1” or the full month name “January“.

1. Converting Month Number to Month Name

When we get the month information as a number, suppose 1, we can convert it to the month name in 3 patterns:

  • 1 -> January“: Full name of the month for display purposes, generally.
  • 1 -> JANUARY“: Full name of the month as stored in the java.time.Month enum.
  • 1 -> Jan“: This is the abbreviation (short form) pattern in which month names are 3 characters long.

Let us see Java programs to convert month numbers into month names. It is necessary to use the Locale information to support any localization needs of the application.

Each of these methods uses the Month.of(monthNumber) method to obtain a Month enum instance corresponding to the given month number. Then, the getDisplayName() or name() methods are called with the appropriate TextStyle to retrieve the desired representation of the month.

// 1 - Jan

public static String monthNumberToAbbr(int monthNumber) {

  return Month.of(monthNumber).getDisplayName(
      TextStyle.SHORT, Locale.getDefault()
  );
}

// 1 - January

public static String monthNumberToFullName(int monthNumber) {
  return Month.of(monthNumber).getDisplayName(
      TextStyle.FULL, Locale.getDefault()
  );
}

// 1 - JANUARY

public static String monthNumberToName(int monthNumber) {
  return Month.of(monthNumber).name();
}

Let us test these methods and verify the outputs:

System.out.println( monthNumberToAbbr(1) );
System.out.println( monthNumberToFullName(1) );
System.out.println( monthNumberToName(1) );

The program output:

Jan
January
JANUARY

In case we pass an invalid number (less than 1 or greater than 12), the Month.of() throws DateTimeException.

2. Converting Month Name to Month Number

To convert a given month name into the month number, we can reverse the approach based on the following input patterns:

  • January (full name): The Month.valueOf() method takes the month name as a parameter and returns the Month enum which we can use in several ways.
  • Jan (abbreviation): There is no direct support for short names in Month enum so we need to devise own solution. We create an array of short names for all months (by stripping to 3 characters) and then find the input abbreviation in this array using the Stream.findFirst() API.

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

public static int monthAbbrToNumber(String abbreviation) {

  Optional<Month> monthOptional = Arrays.stream(Month.values())
      .filter(month -> month.name().substring(0, 3).equalsIgnoreCase(abbreviation))
      .findFirst();

  return monthOptional.orElseThrow(IllegalArgumentException::new).getValue();
}

Let us test these methods and verify that they are converting the month names into numbers, as designed.

System.out.println( monthNameToNumber("January") );
System.out.println( monthAbbrToNumber("Jan") );

The program output:

1
1

3. Converting Month Apbbrvetation to Full Name

Several times, we may need to convert a given short name of the month to its full name. For example, convert Apr to April or convert Jan to January. The following method combines the approaches in the above sections to achieve this result.

The method assumes that the abbreviation passed as an argument is three characters long and corresponds to the first three characters of the month’s name (ignoring case). Make sure that the input abbreviation is a valid month abbreviation to avoid exceptions.

public static String monthAbbrToFullName(String abbreviation) {

  Optional<Month> monthOptional = Arrays.stream(Month.values())
      .filter(month -> month.name().substring(0, 3).equalsIgnoreCase(abbreviation))
      .findFirst();

  return monthOptional.orElseThrow(IllegalArgumentException::new)
      .getDisplayName(TextStyle.FULL, Locale.getDefault());
}

We can test this method as follows:

System.out.println( monthAbbrToFullName("Jan") );

The program output:

January

4. Conclusion

This Java tutorial discussed several methods that we can use for converting between the month number and month names (abbreviations and full names). You are advised to play with these methods and create more such methods for different input and output patterns.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.