/**
* 根据传给的年周,返回该年该周的第一天和最后一天
* @param year 年
* @param week 周
* @return List<Date> dateList 该年该周的第一天和最后一天
*/
public List<Date> getBeWeekAndEndWeek(int year ,int week){
List<Date> dateList = new ArrayList<Date>();
//先滚动到该年
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,year);
//Sunday为1,Monday为2,以此类推
c.setFirstDayOfWeek(2);
//滚动到周
c.set(Calendar.WEEK_OF_YEAR,week);
//得到该周第一天:
c.set(Calendar.DAY_OF_WEEK,1);
dateList.add(c.getTime());
//最后一天:
c.set(Calendar.DAY_OF_WEEK,7);
dateList.add(c.getTime());
return dateList;
}
/**根据传给的日期,返回该日期在哪一周
* @param date 日期
* @return 该日期在哪一周
*/
public int getWeekOfYear(Date date) {
// TODO Auto-generated method stub
int WeekOfYear=0;
Calendar tmpDate=Calendar.getInstance();
//Sunday为1,Monday为2,以此类推
tmpDate.setFirstDayOfWeek(2);
//设置一年中第一个星期所需的最少天数 大于等于4天才算一周
// tmpDate.setMinimalDaysInFirstWeek(4);
tmpDate.setTime(date);
WeekOfYear=tmpDate.get(Calendar.WEEK_OF_YEAR);
int dayOfYear=0;
dayOfYear=tmpDate.get(Calendar.DAY_OF_YEAR);
//当时年初1至7号(范围有点宽),有可能是返回上年的多少周,因此处理为负数以做区别
if(dayOfYear<=7){
if(WeekOfYear>=52){
WeekOfYear=-WeekOfYear;
}
}
return WeekOfYear;
}