填写阵列中的Android递增日期值 [英] Fill Array With incremented Date value in Android
问题描述
//嗯,我已经经历了这么多的教程走了,但coudlnt找到我的事情,所以我在这里把我的查询
我有两个的DatePicker
寄件者
和 TODATE
我要的是当用户selext之日起就应该在字符串数组
的日期
假设我的日期是 START_DATE =2012年1月9日
我要填写阵列像
//Well i have gone through so many tutorial but coudlnt find my thing so i am here to put up my query
i have two DatePicker
FromDate
and ToDate
What i want is when User selext the date it should come in a String Array
of Dates
suppose my date is Start_Date="1/09/2012"
i want to fill Array like
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();
}
我得到一个空指针异常错误
i am getting a null pointer Exception Error
这是给我的错误请帮助!
it is giving me error Please help !!
推荐答案
创建的startDate和结束日期这样的
Create startDate and endDate like this
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。
This method is generating an 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;
}
使用此格式化日期字符串
Use this to format the date string
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;
}
这篇关于填写阵列中的Android递增日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!