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;
}
获取当前周、月、指定时间段内的所有日期
最新推荐文章于 2024-04-01 09:27:44 发布