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!
Great Example. I was looking for this everywhere. 🙂
thanks
This is great. How did you find it?
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:
Thank you ,this was really helpful
Too good…:)
I used the boring switch statement before..i ll replace it with this1
thanks!
Hey this is really good!!! was tired of array and switch statments..
Thank You guy..
U save my life hehe
that’s realy realy good….
thanks you again
=)
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
My Code was wrote in spanish……returns month name in spanish if p_idioma = 0 or english if p_idioma = 1
Very good and useful example.
Very useful indeed…thanks
Absolutely Fantastic Code , Thanks alot, & give more nice codes like this.
Thank you. It is very useful and handy
Thanks av been looking for this
your code dnt run
Thank U v m
Thanks a ton 🙂 cannot imagine a world where we don’t get such articles easily available for our use. You guys are great.
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
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
After 8 years……….still in demand!!!!!!!!!!!!
Thanx dude
Really simple. No words to thank.
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)
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!
nice piece of code.
very nice yar good Thanks a lot
For more Java Date and Calendar Examples and Conversions please go through this blog http://adnjavainterview.blogspot.in/2014/08/java-date-and-calendar-examples.html.
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.