1 Star 4 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
一样的兄弟

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

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

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

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

看了一下,虚拟机同步了本机时间,导致utc时间有误。取消时间同步就好了。属于错在了意向不到的地方。

之前也以为是时区问题,后面发现redis不论机器时区是啥只看utc。

111

scarf 任务状态待办的 修改为已完成
scarf 任务状态已完成 修改为进行中
scarf 任务状态进行中 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
9997491 karasukaigan 1664448219 9719256 wjsr 1712210038 10456722 hestudio admin 1722736254 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

搜索帮助

A270a887 8829481 3d7a4017 8829481