其他分享
首页 > 其他分享> > 获取当前日期的本周的星期一到星期日的日期

获取当前日期的本周的星期一到星期日的日期

作者:互联网

/**
* 获取当前日期的本周的星期一到星期日的日期,指定时分
*   date:当前日期

*    hour:指定的小时     min:指定的分钟
* @author Sven
*
*/
public class WeekUtil {

public static Date[] getMonday(Date date, String hour, String min) {
  Calendar c = Calendar.getInstance();

  c.setTime(date);
  // 星期:1,2,3,4,5,6,7  
  int iWeek = c.get(Calendar.DAY_OF_WEEK);
  // 时期日->星期八  
  if (iWeek == 1) {
    iWeek = 8;
  }
  // 算出离星期一(2)的距离  
  Date mon = DateUtils.addDays(date, 2 - iWeek);
  // 算出离星期日(8)的距离  
  Date sun = DateUtils.addDays(date, 3 - iWeek);
  Date sun3 = DateUtils.addDays(date, 4 - iWeek);
  Date sun4 = DateUtils.addDays(date, 5 - iWeek);
  Date sun5 = DateUtils.addDays(date, 6 - iWeek);
  Date sun6 = DateUtils.addDays(date, 7 - iWeek);
  Date sun7 = DateUtils.addDays(date, 8 - iWeek);
  Date[] dates = new Date[7];
  dates[0] = mon;
  dates[1] = sun;
  dates[2] = sun3;
  dates[3] = sun4;
  dates[4] = sun5;
  dates[5] = sun6;
  dates[6] = sun7;
  for (int i = 0; i < dates.length; i++) {
    c.setTime(dates[i]);
    c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
    c.set(Calendar.MINUTE, Integer.valueOf(min));
    c.set(Calendar.SECOND, 0);
    dates[i] = c.getTime();
  }

  return dates;
}

标签:星期日,dates,星期一,addDays,DateUtils,Date,日期,date,iWeek
来源: https://www.cnblogs.com/maowang123/p/12029194.html