赞
踩
public static int getWeekByDate(Date startTime,Date endTime){ Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(startTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endTime); //总周次 int sumWeek = 0; //判断是否同一年 if(startCalendar.get(Calendar.YEAR)==startCalendar.get(Calendar.YEAR)){ //同一年,计算两个周次相减 sumWeek = endCalendar.get(Calendar.WEEK_OF_YEAR) - startCalendar.get(Calendar.WEEK_OF_YEAR)+1; }else{ //1.起始年剩余的周次 //2.终止年的周次 //3.中间经历过的年的总周次 //1 sumWeek+=(startCalendar.getWeeksInWeekYear()-startCalendar.get(Calendar.WEEK_OF_YEAR)+1); //2 sumWeek+=endCalendar.get(Calendar.WEEK_OF_YEAR); //3 int year = endCalendar.get(Calendar.YEAR)-startCalendar.get(Calendar.YEAR)-1; for (int i = 0; i < year; i++) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR,startCalendar.get(Calendar.YEAR)+i); sumWeek+=calendar.getWeeksInWeekYear(); } } return sumWeek; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。