Android
Android

Reputation: 398

Month name from Month Numbers in android

I have a problem with converting the month number into month Name that is if month number 1 or 2 its returning March only. But for 1 it should return Feb right ? Previously i had this same problem for one day but next day it automatically worked i don;t How? But today again its showing like this I need some help to FIX it

public static String getMonthShortName(int monthNumber) {
    String monthName = "";

    if (monthNumber >= 0 && monthNumber < 12)
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, monthNumber);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
            //simpleDateFormat.setCalendar(calendar);
            monthName = simpleDateFormat.format(calendar.getTime());
        } catch (Exception e) {
            if (e != null)
                e.printStackTrace();
        }
    return monthName;
}

if monthNumber 1 or 2 in this statement

monthName = simpleDateFormat.format(calendar.getTime());

its returning Mar (March) only. But for the other numbers its working fine. Can any one of help me out of this ?

Upvotes: 11

Views: 15427

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338326

tl;dr

java.time.Month.of ( monthNumber )   // Get `Month` enum object for month number, 1-12 for January-December. 
.getDisplayName (                    // Generate localized text for name of this month.
    TextStyle.FULL_STANDALONE ,      // Month name spelled out, not abbreviated. 
    Locale.of ( "en" , "US" )        // English language, United States cultural norms.
)                                    // Returns string with localized text such as “January”. 

See this run:

January

java.time.Month

Use only the modern java.time classes in your date-time work. Never use Calendar, Date, SimpleDateFormat, etc.

Android API Level 26+ comes with an implementation of java.time. For earlier Android, the latest tooling provides most of the functionality via API desugaring.

To represent a month, use the Month enum. Get an enum object for your month number. In contrast to the legacy date-time classes, java.time uses sane numbering: 1-12 for January-December.

Month month = Month.of ( monthNumber ) ;  // 1-12 for January-December.

Generate localized text for name of month

Generate text in automatically-localized format using Month#getDisplayName.

  • Pass a TextStyle object to specify how long or abbreviated you want the name.
  • Pass a Locale to determine the human language and cultural norms to be used in localizing. For English language with United States cultural norms, use en & US.
TextStyle style = TextStyle.FULL_STANDALONE ;
Locale locale = Locale.of ( "fr" , "CA" ) ;  // French language, Canada cultural norms. 
// In earlier Java versions: Locale locale = Locale.forLanguageTag ( "fr-CA" ) ;
String output = month.getDisplayName ( style , locale ) ;

See this code run at Ideone.com.

janvier

Upvotes: 1

Sandeep Pareek
Sandeep Pareek

Reputation: 1789

in Kotlin

fun getMonthByNumber(monthnum:Int):String {
        val c = Calendar.getInstance()
        val month_date = SimpleDateFormat("MMMM")
        c[Calendar.MONTH] = monthnum-1
        return month_date.format(c.time)
    }

thank you

Upvotes: 3

Anchit Mittal
Anchit Mittal

Reputation: 3426

Following code for returning the right month name.

Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
int monthnum=5;
cal.set(Calendar.MONTH,monthnum);
String month_name = month_date.format(cal.getTime());

Log.e("",""+month_name);

Here the output is June

Upvotes: 15

cosmincalistru
cosmincalistru

Reputation: 1325

The issue appears because February has less than 30 days.

For example, running this today (30.04.2014) :

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 1);
System.out.println(calendar.getTime());

returns

Sun Mar 02 14:52:28 CET 2014

You should add in your code before setting month :

calendar.set(Calendar.DAY_OF_MONTH, 1);

Upvotes: 4

Related Questions