DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
List<SatisticsByCustomer> list=null;
try{
String beginTime="2013-01-01";
String endTime="2013-01-21";
Calendar startDay = Calendar.getInstance();
Calendar endDay = Calendar.getInstance();
startDay.setTime(FORMATTER.parse(beginTime));
endDay.setTime(FORMATTER.parse(endTime));
// 给出的日期开始日比终了日大则不执行打印
list=new ArrayList<SatisticsByCustomer>();
SatisticsByCustomer s1=new SatisticsByCustomer();
s1.setColumnName(beginTime);
list.add(s1);
if(!beginTime.equals(endTime)){
if(startDay.compareTo(endDay)<=0){
//现在打印中的日期
Calendar currentPrintDay = startDay;
while (true){
SatisticsByCustomer s=new SatisticsByCustomer();
// 日期加一
currentPrintDay.add(Calendar.DATE, 1);
// 日期加一后判断是否达到终了日,达到则终止打印
if (currentPrintDay.compareTo(endDay) == 0) {
break;
}
s.setColumnName(FORMATTER.format(currentPrintDay.getTime()));
list.add(s);
}
SatisticsByCustomer s2=new SatisticsByCustomer();
s2.setColumnName(endTime);
list.add(s2);
}
}
for (SatisticsByCustomer customer : list)
{
System.out.println(customer.getColumnName());
}
}catch (Exception e){
e.printStackTrace();
}
java根据开始时间结束时间计算中间间隔日期
最新推荐文章于 2024-07-26 03:05:11 发布