java 中怎么根据当前时间得到上周一和上周五的日期

/**
     * 获取上周五时间
     */
    public Date lastFirday() {
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 7 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 9);

        return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);

    }

 

/**
     * 获取上周一时间
     */
    public Date lastMonday() {
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 1 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 7);
        return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);
    }

 

/**
  * 得到某一天的该星期的第一日 00:00:00
  *
  * @param date
  * @param firstDayOfWeek
  *            一个星期的第一天为星期几
  *
  * @return
  */
 public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
  Calendar cal = Calendar.getInstance();
  if (date != null)
   cal.setTime(date);
  cal.setFirstDayOfWeek(firstDayOfWeek);
  cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return cal.getTime();
 }

java 中怎么根据当前时间得到上周一和上周五的日期

上一篇:共享变量/全局变量/datasocket


下一篇:FineUI v4.0.2 (beta) 发布了!