获取当前周、月、指定时间段内的所有日期

 public Integer saveLoginCount(int userId,String startTime, String endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String key = "login:" + format;
        jedis.setbit(key.getBytes(), userId, true);
//        获取固定周期的在线人数
        int currentWeekOnlineCount = 0;
        List<String> timeInterval = getDays(startTime,endTime);

        for (int i = 0; i < timeInterval.size(); i++) {
            BitSet b3 = new BitSet();
            if (jedis.get(("login:" + timeInterval.get(i)).getBytes()) != null) {
                b3 = fromByteArrayReverse(jedis.get(("login:" + timeInterval.get(i)).getBytes()));
            }
            //        统计当天在线过的用户个数
            int cardinality3 = b3.cardinality();
            logger.info(timeInterval.get(i) + "登录总数为:" + cardinality3);
            currentWeekOnlineCount += cardinality3;
        }
        logger.info("当前时间段的总在线人数:" + currentWeekOnlineCount);
        return currentWeekOnlineCount;
    }

    /**
     * 解决redis 的 bitmap 转换成 Java 的 bitset 采用 BitSet.valueOf 方法获取出来的结果跟存储进去的不一样的问题
     *
     * @param bytes
     * @return
     */
    public BitSet fromByteArrayReverse(final byte[] bytes) {
        final BitSet bits = new BitSet();
        for (int i = 0; i < bytes.length * 8; i++) {
            if ((bytes[i / 8] & (1 << (7 - (i % 8)))) != 0) {
                bits.set(i);
            }
        }
        return bits;
    }

    /**
     * 给定时间段获取所有日期
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (Exception e) {
           logger.info("日期转换失败。");
        }

        return days;
    }

    /**
     * 获取当前月份的所有日期字符串
     *
     * @return
     */
    public List<String> getCurrentMonths() {
        List<String> currentMonths = new ArrayList<>();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        String lastDay = sf.format(cal.getTime());
        String aDay = "";
        int i = 1;
        while (!lastDay.equals(aDay)) {
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), i);
            aDay = sf.format(cal.getTime());
//            System.out.println(sf.format(cal.getTime()));
            currentMonths.add(sf.format(cal.getTime()));
            i++;
        }
        return currentMonths;
    }

    /**
     * 获取传递日期所在的周的所有日期字符串
     *
     * @return
     */
    public List<String> getTimeInterval(Date date) {
        List<String> currentWeeks = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
//      判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
//      设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
//      获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
//      根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        currentWeeks.add(imptimeBegin);
        for (int i = 0; i < 6; i++) {
            cal.add(Calendar.DATE, 1);
            currentWeeks.add(sdf.format(cal.getTime()));
        }
        return currentWeeks;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值