tags:

views:

209

answers:

3

I wanna programmatically convert an integer in the range 1-12 to corresponding month name. (e.g. 1 -> January, 2 -> February) etc using Java Calendar class in one statement.

Note : I want to do it using Java Calendar class only. Don't suggest any switch-case or string array solution.

Thanks.

+3  A: 

Use DateFormatSymbols

Proudly copied and pasted from bluebones.net:

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;
}
Anurag
Using Calendar, the question says.
Red Hyena
then use getDisplayName as camickr suggests
Anurag
I got the answer from coolbird. Your solution is also good ! I +1'ed it cuz it, if modified a bit, satisfies one-statement requirement! :)
Red Hyena
getDisplayName() in Calendar class is a better solution, and I think it might be using DateFormatSymbols class underneath.
Anurag
@Anurag : I checked src of Calendar. The method getDisplayName() indeed uses DateFormatSymbols underneath. +1 from me. :)
missingfaktor
+1  A: 

Did you read the API? The method getDisplayName(...) looks like a good place to start. Doing it in one statement is a terrible requirement.

camickr
+7  A: 

The Calendar class is not the best class to use when it comes obtaining the localized month name in one statement.

The following is an example of obtaining the month name of a desired month specified by a int value (where January is 1), using only the Calendar class:

// Month as a number.
int month = 1;

// Sets the Calendar instance to the desired month.
// The "-1" takes into account that Calendar counts months
// beginning from 0.
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, month - 1);

// Returns a String of the month name in the current locale.
c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

The above code will return the month name in the system locale.

If another locale is required, one can specify another Locale by replacing the Locale.getDefault() with a specific locale such as Locale.US.

coobird
Though not in one statement, this is a good enough answer for me! Thanks!
Red Hyena
I was actually looking at the getDisplayNames() method. Any idea why it returns a Map<String, Integer>? I would think Map<Integer, String> would be more logical since you could access any month with the appropriate Integer value. I can't seen any requirement to access the Map with a month name to get its integer value. I guess thats what you mean when you say its not the best class to use to get the month.
camickr
@camickr: The `getDisplayNames` method seems like it has been retrofitted to the `Calendar` class to support some new use-case that arose from an addition of another class. I would suspect `DateFormatSymbols` as it seems like many parts of that class and the aforementioned method has been added in JDK 1.6.
coobird
@coobird, I didn't look at the method close enough and I see that when using Styles.ALL_STYLES the method returns both short and long descriptions so to keep the key unique, you need the use the month as a String, not an Integer.
camickr