1 Star 1 Fork 1

scarf/springboot3-springsecurity6-jwt

 / 详情

redis 时区问题

待办的
拥有者
创建于  
2024-04-05 18:51

redisTemplate.expireAt 设置 Date 日期后与实际相差八小时。

评论 (10)

scarf 创建了任务
scarf 修改了标题
scarf 修改了标题
scarf 修改了描述
scarf 添加了
 
question
标签
scarf 负责人设置为scarf
scarf 负责人scarf 修改为未设置
scarf 负责人设置为scarf
scarf 负责人scarf 修改为未设置
展开全部操作日志

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

// ...

Date originalDate = // 原始日期
ZonedDateTime originalZonedDateTime = originalDate.toInstant().atZone(ZoneId.systemDefault());
ZonedDateTime utcZonedDateTime = originalZonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
Date utcDate = Date.from(utcZonedDateTime.toInstant());

redisTemplate.expireAt(key, utcDate);

单元测试
缓存TTL
行不通,实际没有相差一天。

public void expireAt(String key, Date expire) {
    Instant instant = expire.toInstant();
    ZonedDateTime utcDateTime = instant.atZone(ZoneId.of("UTC"));
    redisTemplate.expireAt(key, new Date(utcDateTime.toInstant().toEpochMilli()));
}

bc60ae3d_9719256
cd2845ff_9719256
一样的兄弟

  • 本机环境: Mac M1
  • docker 镜像: redis:6.0.8

Date now = new Date();
now = DateUtils.addDay(now ,1);
redisTemplate.expireAt("test", now );

再或者 直接 计算一天后的毫秒数, 直接设置过期时间 1000 * 60 *60 *24

指定 ttl 确实可以,不过要是想要正确设置 Date 估计得看系统吧,之前在 window 上可行

redis里面的日期没有设置时区或者是你服务器没有设置时区? 你这个正好差八小时 不就是 北京时间或者上海时间?

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
9997491 karasukaigan 1664448219 9719256 wjsr 1712210038 10456722 hestudio admin 1655123533 5091950 acaterpillar 1650879772
Java
1
https://gitee.com/wjsr/springboot3-springsecurity6-jwt.git
git@gitee.com:wjsr/springboot3-springsecurity6-jwt.git
wjsr
springboot3-springsecurity6-jwt
springboot3-springsecurity6-jwt

搜索帮助

344bd9b3 5694891 D2dac590 5694891