JAVA 根据具体年份周数获取日期范围

/**
* 根据具体年份周数获取日期范围
* @param year
* @param week
* @param targetNum
* @return
*/
public String getWeekDays(int year, int week, int targetNum) {
 

  // 计算目标周数
  if (week + targetNum > 52) {
    year++;
    week += targetNum - 52;
  } else if (week + targetNum <= 0) {
    year--;
    week += targetNum + 52;
  } else {
    week += targetNum;
  }

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  Calendar cal=Calendar.getInstance();

  // 设置每周的开始日期
  cal.setFirstDayOfWeek(Calendar.SUNDAY);

  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.WEEK_OF_YEAR, week);

  cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
  String beginDate = sdf.format(cal.getTime());

  cal.add(Calendar.DAY_OF_WEEK, 6);
  String endDate = sdf.format(cal.getTime());

  return beginDate + "~" + endDate;
}

posted @ 2018-04-08 17:58  mithrandirw  阅读(8892)  评论(1编辑  收藏  举报