-3

Can someone help me out in this.I need to find the month in words of a particular date in android.I am using the following code to get the day of the week(I have passed the parameters of the day.) by not understanding how to get the month. This returns the day of the week :

public static String getFullDayName(int day_,int year_,int month_) 
    {
        Calendar c = Calendar.getInstance();
        // date doesn't matter - it has to be a Monday
        // I new that first August 2011 is one ;-)
        c.set(year_, month_-1, 1, 0, 0, 0);
        // c.set(2013,7,03);
        c.add(Calendar.DAY_OF_MONTH,day_-1);
        return String.format("%tA", c);
    }

I have tried the following code to get the month of the year but not getting the answer(month).but gives me the day of week instead.

Joyson
  • 1,643
  • 5
  • 28
  • 48

2 Answers2

1

Update your method as below

public static String getFullDayName(int day_,int year_,int month_) 
    {
        Calendar c = Calendar.getInstance();
        // date doesn't matter - it has to be a Monday
        // I new that first August 2011 is one ;-)
        c.set(year_, month_-1, 1, 0, 0, 0);
        // c.set(2013,7,03);
        c.add(Calendar.DAY_OF_MONTH,day_-1);

        // Option 1 
        // String month = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US); // Returns like August
        // Option 2 
        // String month = c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US); // Returns like Aug      

        // And other option can be      
        String month = (String) android.text.format.DateFormat.format("MMM", c.getTime()); // Returns like Aug

        return month;
    }
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0

try

 int month = c.get(Calendar.MONTH);
Sebastian Baltes
  • 512
  • 5
  • 14