根据某年的第几周查询出这个周的开始时间和结束时间

//获取这一年的日历
public static Calendar getCalendarFormYear(Integer year) {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  cal.set(Calendar.YEAR, year);
  return cal;
}

//获取某一年的某一周的周日日期
public static String getEndDayOfWeekNo(Integer year, Integer weekNo) {
  Calendar cal = getCalendarFormYear(year);
  cal.set(Calendar.WEEK_OF_YEAR, weekNo);
  cal.add(Calendar.DAY_OF_WEEK, 6);
  return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
}

//获取某一年的某一周的周一日期
public static String getStartDayOfWeekNo(Integer year, Integer weekNo) {
  Calendar cal = getCalendarFormYear(year);
  cal.set(Calendar.WEEK_OF_YEAR, weekNo);
  return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
}

//测试
public static void main(String[] args) {
  System.out.println(SaleplanServiceImpl.getStartDayOfWeekNo(2021, 1));
  System.out.println(SaleplanServiceImpl.getEndDayOfWeekNo(2021, 1));
  System.out.println(SaleplanServiceImpl.getCalendarFormYear(2021));
}


//根据指定的日期来获取到,这个日期是当年的第几周。
public static void main(String[] args) {
  String today = "2019-07-15";
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  Date date = null;
  try {
    date = format.parse(today);
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  Calendar calendar = Calendar.getInstance();
  calendar.setFirstDayOfWeek(Calendar.MONDAY); //设置周开始是星期几
  calendar.setTime(date);
  System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
  System.out.println(SaleplanServiceImpl.getEndDayOfWeekNo(2019, week));
}

posted @ 2021-12-16 11:26  丶Ronnie  阅读(223)  评论(0)    收藏  举报