如何将while循环转换为arraylist

时间:2015-04-10 13:19:09

标签: java arraylist

我将以下while循环转换为arraylist时遇到困难,因此我可以在selectonemenu组件中显示值。 while循环代码是:

        String date1 = "JAN-2015";
        String date2 = "APR-2015";

        DateFormat formater = new SimpleDateFormat("MMM-yyyy");

        Calendar beginCalendar = Calendar.getInstance();
        Calendar finishCalendar = Calendar.getInstance();


            beginCalendar.setTime(formater.parse(date1));
            finishCalendar.setTime(formater.parse(date2));


        while (beginCalendar.before(finishCalendar)) {
            String date =     formater.format(beginCalendar.getTime()).toUpperCase();
            System.out.println(date);
            beginCalendar.add(Calendar.MONTH, 1);
}

1 个答案:

答案 0 :(得分:2)

ArrayList<String> dateSelections = new ArrayList<>();
while (beginCalendar.before(finishCalendar)) 
{
        String date = formater.format(beginCalendar.getTime()).toUpperCase();
        dateSelections.add(date);
        beginCalendar.add(Calendar.MONTH, 1);
}

日期选择应填充在while循环结束时可能的选择。