您的位置:首页 > 其它

根据开始和结束时间,计算出该时间段每个星期的开始和结束时间

2015-07-03 15:47 375 查看
   /***

     * 根据开始和结束时间,计算出该时间段每个星期的开始和结束时间

     * @return 时间段内的开始和结束时间List

     * */
public  List<String> getWeekByTime(String startDate,String endDate){
Calendar c_begin = new GregorianCalendar();
    Calendar c_end = new GregorianCalendar();
    DateFormatSymbols dfs = new DateFormatSymbols();
    List<String> timeList = new ArrayList<String>();
    c_begin.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1])-1, Integer.parseInt(startDate.split("-")[2])-1); //Calendar的月从0-11,所以4月是3.
    c_end.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1])-1, Integer.parseInt(endDate.split("-")[2])-1); //Calendar的月从0-11,所以5月是4.
    int count = 1;
    c_end.add(Calendar.DAY_OF_YEAR, 1);  //结束日期下滚一天是为了包含最后一天
    int startWeek=0;
  int endWeek=0;
  String startTime="";
  String endTime="";
    while(c_begin.before(c_end)){
    if(count==1){
    if(c_begin.get(Calendar.DAY_OF_WEEK)==1){
    timeList.add(startDate+"&"+new java.sql.Date(c_begin.getTime().getTime())+"");
   
}
    }else{
    if(c_begin.get(Calendar.DAY_OF_WEEK)==2){
    startWeek=count;
    startTime=new java.sql.Date(c_begin.getTime().getTime())+"";
    }
     
    if(c_begin.get(Calendar.DAY_OF_WEEK)==1){
    endWeek=count;
    endTime=new java.sql.Date(c_begin.getTime().getTime())+"";
    }
     
    if(startWeek==endWeek){
    timeList.add(startTime+"&"+endTime+"&"+"第"+count+"周 ");
    }
    }
     if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
         count++;
     }
     c_begin.add(Calendar.DAY_OF_YEAR, 1);
    }
    return timeList;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: