1

//好吧,我经历了这么多教程,但找不到我的东西,所以我在这里提出我的查询,我有两个DatePicker FromDateToDate 我想要的是当用户选择它应该进入String Array的日期时,假设我的日期是Start_Date="1/09/2012"我想要的像填充数组

String[] Date Arrau= {"1/09/2012","2/09/2012","3/09/2012","4/09/2012"} 

我所做的是

    DateDifference=Integer.parseInt(EndDate)-Integer.parseInt(StartDate);

SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");

    Date myDate= null;
    try {
        myDate= sdf.parse(StartDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Date newFirstDate= new Date(myDate.getDate()); // 7 * 24 * 60 * 60 * 1000

    for(int i=0;i<=DateDifference;i++){
        Calendar c= Calendar.getInstance();
        c.setTime(newFirstDate);
        c.add(Calendar.DATE, 1);
        Date newDate= c.getTime();  
        DateArray[i]=newDate.toString();

    }

我得到一个空指针异常错误

它给了我错误请帮助!

4

1 回答 1

4

像这样创建 startDate 和 endDate

   Calendar startDate, endDate;
   startDate = Calendar.getInstance();
   endDate = Calendar.getInstance();

        startDate.clear();
        endDate.clear();

        startDate.set(2012, 8, 1);
        endDate.set(2012, 8, 30); 
        ArrayList<String> list = getList(startDate, endDate);

此方法正在生成一个 arrayList。

public ArrayList<String> getList(Calendar startDate, Calendar endDate) {
        ArrayList<String> list = new ArrayList<String>();
        while (startDate.compareTo(endDate) <= 0) {
                    list.add(getDate(startDate));
            startDate.add(Calendar.DAY_OF_MONTH, 1);
        }
        return list;

    }

使用它来格式化日期字符串

public String getDate(Calendar cld) {

        String curDate = cld.get(Calendar.YEAR) + "/" + (cld.get(Calendar.MONTH) + 1) + "/"
                + cld.get(Calendar.DAY_OF_MONTH);
        try {
            Date date = new SimpleDateFormat("yyyy/MM/dd").parse(curDate);

            curDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return curDate;
    }
于 2012-09-26T09:07:08.040 回答