Android has been creating ArrayList since several months

I want to populate arraylist name with month names. I am getting the month numerically from the database. Now I want to convert the month number to month and add to an array. I tried it but it seems to work. I receive every month with the same name.

int[] months = new int[count];
for(int n=0; n<count; n++) {
            c.moveToNext();     
            months[n]= c.getInt(0);

 ArrayList<String> xVals = new ArrayList<String>();
            for (int i = 0; i <months.length; i++) {    
                Calendar cal=Calendar.getInstance();
                SimpleDateFormat month_date = new SimpleDateFormat("MMMM");                 
                cal.set(Calendar.MONTH,months[i]);
                String month_name = month_date.format(cal.getTime());

                   xVals.add(month_name);   

            }
}

      

+3


source to share


2 answers


There's only 12 months in there, can't you just hard-code the values โ€‹โ€‹of the months array?

try it:



    ArrayList<String> months = new ArrayList<String>();

    for (int i = 0; i < 12; i++) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
        cal.set(Calendar.MONTH, i);
        String month_name = month_date.format(cal.getTime());

        months.add(month_name);
    }

      

+1


source


Easy:

DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] monthNames = symbols.getMonths(); 

      



http://developer.android.com/reference/java/text/DateFormatSymbols.html

+17


source







All Articles