Java Data Type How to - Get Month name from int








Question

We would like to know how to get Month name from int.

Answer

import java.time.LocalDate;
//from ww w  . ja  v  a  2 s.  c  o  m
public class Main {
  public static void main(String[] argv) {
    System.out.println(getMonthName(2));
  }

  public static String getMonthName(Integer month) {
    LocalDate localDate = LocalDate.of(0, month, 0);
    return localDate.getMonth().name();
  }
}

The code above generates the following result.