Java Data Type How to - Get month name by month number








Question

We would like to know how to get month name by month number.

Answer

/*from w  w w .  j  a  v  a2 s .co  m*/
public class Main {

  public static void main(String[] args) {
    System.out.println(getMonthByCode(2));
  }

  public static String getMonthByCode(int month) {
    if (month == 1) {
      return "Enero";
    }
    if (month == 2) {
      return "Febrero";
    }
    if (month == 3) {
      return "Marzo";
    }
    if (month == 4) {
      return "Abril";
    }
    if (month == 5) {
      return "Mayo";
    }
    if (month == 6) {
      return "Junio";
    }
    if (month == 7) {
      return "Julio";
    }
    if (month == 8) {
      return "Agosto";
    }
    if (month == 9) {
      return "Septiembre";
    }
    if (month == 10) {
      return "Octubre";
    }
    if (month == 11) {
      return "Noviembre";
    }
    if (month == 12) {
      return "Diciembre";
    } else {
      return null;
    }
  }
}

The code above generates the following result.