java 求取某一段时间内的每一天、每一月、每一年

1.求取某一段时间内的每一天

     Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01");
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-10-03");
Calendar cal = Calendar.getInstance();
cal.setTime(date0);
while(cal.getTime().compareTo(date1)<=0){
cal.add(Calendar.DAY_OF_MONTH,1);
list0.add(sdf.format(cal.getTime()));
}

2.求取某一段时间内的每一月
方法一:

date0 = new SimpleDateFormat("yyyy/MM/dd").parse("2014/01/01");
Date date1 = new SimpleDateFormat("yyyy/MM/dd").parse("2014/10/03");
Calendar cal = Calendar.getInstance();
cal.setTime(date0);
while(cal.getTime().compareTo(date1)<=0){
cal.add(Calendar.DAY_OF_MONTH,1);
list0.add(sdf.format(cal.getTime()));
}

方法2:

 /**
* 求取月分
*/
List<String> list01 = new ArrayList<String>();
Date begin = sdf.parse("2014/01/01");
Date end = sdf.parse("2014/10/03");
Calendar cal1 = Calendar.getInstance();
cal1.setTime(begin);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(end);
int mouths = cal2.get(Calendar.MONDAY)- cal1.get(Calendar.MONTH);//获取年份
for (int i = 0; i <= mouths; i++) {
cal1.add(Calendar.MONTH,1);
list01.add(sdfq.format(cal1.getTime()).trim());
}

3.求取每一年
方法一:

 //方法一
date0 = new SimpleDateFormat("yyyy").parse("2010");
Date date1 = new SimpleDateFormat("yyyy").parse("2014");
Calendar cal = Calendar.getInstance();
cal.setTime(date0);
while(cal.getTime().compareTo(date1)<=0){
list0.add(sdf.format(cal.getTime()));
cal.add(Calendar.YEAR,1); }

方法二:

 Date begin = sdf.parse("2014");
// Date end = sdf.parse("2014");
// Calendar cal1 = Calendar.getInstance();
// cal1.setTime(begin);
// Calendar cal2 = Calendar.getInstance();
// cal2.setTime(end);
// int mouths = cal2.get(Calendar.YEAR)- cal1.get(Calendar.YEAR);//获取年份
// for (int i = 0; i <= mouths; i++) {
// if(i!=0){
// cal1.add(Calendar.YEAR,1);
// }
// list02.add(sdfp.format(cal1.getTime()).trim());
// }
上一篇:Qt QString的arg()方法的使用


下一篇:前端MVVM学习之KnockOut(二)