2012-09-26 170 views
0

遞增日期值//好吧,我已經經歷了這麼多的教程走了,但coudlnt找到我的事情,所以我在這裏把我的查詢 我有兩個DatePickerFromDateToDate 我要的是當用戶selext日期應該進來日期的String Array 想我的日期是Start_Date="1/09/2012"我要填寫陣列像填充陣列中的Android

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

    } 

我得到一個空指針異常錯誤

它給我錯誤請幫助!

+1

請註明你所得到的錯誤!而你的問題是不明確的。你對Start_Date對象意味着什麼?你究竟想要什麼。請完善你的問題。 – Hiral

+0

@手機請查看我更新的問題 –

回答

4

這樣

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

創建的startDate和結束日期此方法生成的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; 
    } 
+0

如果我不想發送「yyyy/MM/dd」中的DateFormat它應該是dd/MM/yyyy 它感謝您的幫助謝謝! –

+0

我已經找到另一個intresting的答案 請看看http://stackoverflow.com/questions/2689379/how-to-get-a-list-of-dates-between-two-dates-in-java –