论坛首页 Java企业应用论坛

将指定日期段转换为星期,存入到List

浏览 2102 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-08-13  
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c_begin = new GregorianCalendar();
Calendar c_end = new GregorianCalendar();
DateFormatSymbols dfs = new DateFormatSymbols();
String[] weeks = dfs.getWeekdays();
c_begin.setTime(sdf.parse(form.getStartDate())); //开始日期
c_end.setTime(sdf.parse(form.getEndDate()));     //结束日期
c_end.add(Calendar.DAY_OF_YEAR, 1);  //结束日期下滚一天是为了包含最后一天
List week=new ArrayList();     //存放一周
List weekList=new ArrayList(); //存放所有星期的List
while(c_begin.before(c_end)){
   if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY || c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.TUESDAY || c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY || c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.THURSDAY || c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY){
        week.add(new java.sql.Date(c_begin.getTime().getTime()));
}
    if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
weekList.add(week);
week=new ArrayList();
}
    c_begin.add(Calendar.DAY_OF_YEAR, 1); //加最后一周
   }
 
}
weekList.add(week);
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics