美文网首页
根据日期获取日,周,月范围

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

作者: 王小杰at2019 | 来源:发表于2020-02-16 23:05 被阅读0次
    /**
         * 获取一天 的时间范围
         *
         * @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};
        }
    
    

    相关文章

      网友评论

          本文标题:根据日期获取日,周,月范围

          本文链接:https://www.haomeiwen.com/subject/dulvfhtx.html