根据日期获取日,周,月范围

/**
     * 获取一天 的时间范围
     *
     * @param date
     * @return
     */
    public static Date[] getDayRange(Date date) {
        Date begin = DateUtils.truncate(date, Calendar.DATE);
        Date end = DateUtils.addDays(begin, 1);
        return new Date[]{begin, end};
    }

    /**
     * 根据当天获取所在周的区间
     *
     * @param date
     * @return
     */
    public static Date[] getWeekRange(Date date) {
        date = DateUtils.truncate(date, Calendar.DATE);
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        // 周日1  周六是7
        int diff = 7 - i;
        Date start = DateUtils.addDays(date, i * -1 + 1);
        Date end = DateUtils.addDays(date, diff + 1);
        return new Date[]{start, end};
    }

    /**
     * 根据当天获取所在月的区间
     *
     * @param date
     * @return
     */
    public static Date[] getMonthRange(Date date) {
        Date begin = DateUtils.truncate(date, Calendar.DATE);
        Calendar instance = Calendar.getInstance();
        instance.setTime(begin);
        // 获取今天几号
        int day = instance.get(Calendar.DAY_OF_MONTH);
        // 获取一个月最多多少天
        int totalDay = instance.getActualMaximum(Calendar.DATE);
        // 周日1  周六是7
        int diff = totalDay - day;
        Date start = DateUtils.addDays(begin, day * -1 + 1);
        Date end = DateUtils.addDays(begin, diff + 1);
        return new Date[]{start, end};
    }

你可能感兴趣的:(根据日期获取日,周,月范围)