【问题标题】:Android create ArrayList from months nameAndroid 从月份名称创建 ArrayList
【发布时间】:2015-01-09 23:39:18
【问题描述】:

我想用月份的名称填充一个数组列表。我从数据库中得到数字格式的月份。现在我想将月份数字转换为月份名称并添加到数组中。我试过了,但它似乎工作。我每个月都用相同的名字。

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);   

            }
}

【问题讨论】:

    标签: android string arraylist


    【解决方案1】:

    只有 12 个月,你能不能只硬编码月份数组值?

    试试这个:

        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);
        }
    

    【讨论】:

      【解决方案2】:

      轻松:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-12
        • 1970-01-01
        • 2014-06-16
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 2015-05-31
        相关资源
        最近更新 更多