Month name from Month Numbers in android
Asked Answered
B

3

10

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 ?

Blossomblot answered 30/4, 2014 at 12:44 Comment(2)
try this link developer.android.com/reference/java/util/…Spiros
you should have tagged this as a java question also...Hali
H
4

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);
Hali answered 30/4, 2014 at 12:55 Comment(0)
F
15

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

Fructidor answered 30/4, 2014 at 12:59 Comment(3)
this is the correct solution, i am using something similar in my app.Dictatorial
thanks... my code also same this... if i give 5 it will work fine the problem with 1 and 2 only... and i am using my code for past 3 months it worked find. I think this is the second time it showing wrong result for 1 and 2Blossomblot
Could you please set your system time to 31 of March and then call your sequence of code? you might have a surprise...Hali
H
4

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);
Hali answered 30/4, 2014 at 12:55 Comment(0)
J
3

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

Jenna answered 25/3, 2021 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.