//我已经经历了这么多教程但是找到了我的东西所以我在这里提出我的问题
我有两个DatePicker
FromDate
和ToDate
我想要的是当用户选择日期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();
}
我得到一个空指针异常错误
它给了我错误请帮助!!
答案 0 :(得分: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;
}