Converting Month Number to Month Name In Java!

How long have I wanted to know how to do this! So long so many months! Finally:

import java.text.*;

String getMonthForInt(int m) {
    String month = "invalid";
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    if (m >= 0 && m <= 11 ) {
        month = months[m];
    return month;

Praise the lord! Praise the lord!

27 Replies to “Converting Month Number to Month Name In Java!”

  1. God, I really can’t remember now. It was three years ago! Maybe off the web somewhere or perhaps trawling the standard documentation. I can’t believe I couldn’t find out how to do it for so long! Calendar and GregorianCalendar are so big and confusing I think I kept thinking it was in there somewhere. I really hate dates in java. Not that they are much fun in any language. But java is the worst. I’m not sure I think much of that code with hindsight. Perhaps something a bit more like:

      import java.text.DateFormatSymbols;  public String getMonth(int month) {      return new DateFormatSymbols().getMonths()[month];  }  
  2. Almost 5 years since you wrote this posting and still useful. I must confese i was tempted to write my own solution but now i’ll use that time in seeing the chess tournament games!!! : ) Thanks Thomas

  3. My Code was wrote in spanish……returns month name in spanish if p_idioma = 0 or english if p_idioma = 1

          /**       * Método que retorna el nombre del mes actual del sistema       * @param p_idioma Entero que determina el idioma en que se retornara la respuesta (0 -> español, 1-> ingles)       * @return Nombre del mes actual en el idioma determinado en el parametro       */      public static String getNombreMes(int p_idioma) throws Exception{          String[][] mesNombre = {                                  {"Enero","January"},{"Febrero","Febrary"},{"Marzo","March"},{"Abril","April"},                                  {"Mayo","May"},{"Junio","June"},{"July","Julio"},{"Agosto","August"},                                  {"Septiembre","September"},{"Octubre","October"},{"Noviembre","November"},{"Diciembre","December"},                                  };          /** se verifica que el parametro idioma sea valido **/          if(p_idioma  1) throw new Exception("Error al intentar obtener el nombre del mes con el código de idioma " + p_idioma);          /** se calcula el mes del sistema */          Calendar cal = Calendar.getInstance();          /** se obtiene de la matris el nombre del mes **/          String mes = mesNombre[cal.get(Calendar.MONTH)][p_idioma];          /** se retorna el nombre del mes **/          return mes;        }  
  4. Thanks a ton 🙂 cannot imagine a world where we don’t get such articles easily available for our use. You guys are great.

  5. Hi, Thanks for the beautiful code. Sharing something I found…Calendar Object in Java 6 have methods to show month. calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH); I hope it will work too. Regards Ravi

  6. You could use the VM built in function called String.format take a look at this example: Date date = new Date(); System.out.println(String.format(“%1$tB / %1$tY”,date)); Program output: December / 2011 it’s already conditioned to use Locales getting the default one from your computer. you can also specify a custom Locale there.. Obs.: it works out with Date and Calendar as well

  7. I’m noob in Java but i found this one… calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) this actually returns You string with name of the month in English that You desire 🙂 works for me (change from my language to English)

  8. I would hope that 11 years later they’ve come up with something better 🙂 Looks like getDisplayName was added to Calendar in Java 1.6 (December 2006). I always thought there should be something on Calendar/GregorianCalendar … Good find!

  9. Here is a clean code version for getting the months with an index. This will throw an ArrayIndexOutOfBoundsException when the number sent in isn’t between 0 and 11: public static String getMonthName(int monthIndex) { return new DateFormatSymbols().getMonths()[monthIndex].toString(); } You can also have a better formatted error like this: public static String getMonthName(int monthIndex) { //since this is zero based, 11 = December if (monthIndex > 11 ) { throw new IllegalArgumentException(monthIndex + " is not a valid month index."); } return new DateFormatSymbols().getMonths()[monthIndex].toString(); } Thanks for the original code, it was really helpful.

Leave a Reply

Your email address will not be published.