redis-时间处理

it2023-02-01  47

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”); /** * 当天年月日 / // 创建Date对象,表示当前时间 Date now = new Date(); String key = RedisConst.KEY_RECORD_PHONE + sdf.format(now) + “:USER:” + id; /* * 获取当天的前一天时间 / Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.DATE, -1); Date start = c.getTime(); String beforeKey = RedisConst.KEY_RECORD_PHONE + sdf.format(start) + “:USER:” + id; long beforeValue = redisTemplate.opsForList().size(beforeKey); if (beforeValue>0){ redisTemplate.delete(beforeKey); } long size = redisTemplate.opsForList().size(key); if (size <= 2) { redisTemplate.opsForList().rightPush(key,System.currentTimeMillis() + “”); } else { resultMessage.setErrCode(ResponseEnum.M10095.getCode()); resultMessage.setDesc(ResponseEnum.M10095.getMessage()); return resultMessage; /**List t = redisTemplate.opsForList().range(key, 0, (int) size); Long now = System.currentTimeMillis(); if (now - Long.valueOf(t.get(0)) > 2460601000) {

//最开始的一条距现在超过24小时就移除左边的,并添加一条 redisTemplate.opsForList().leftPop(key); //最开始的一条距现在超过24小时就移除左边的,并添加一条 //redisTemplate.delete(key); redisTemplate.opsForList().rightPush(key,System.currentTimeMillis() + ""); } else {//最左的一条也在24小时内,不能发送短信 resultMessage.setErrCode(ResponseEnum.M10095.getCode()); resultMessage.setDesc(ResponseEnum.M10095.getMessage()); return resultMessage; }**/ }
最新回复(0)