1 Star 1 Fork 1

scarf / springboot3-springsecurity6-jwt

 / 详情

redis 时区问题

Backlog
owner
Opened this issue  
2024-04-05 18:51

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

Comments (5)

scarf created任务
scarf changed title
scarf changed title
scarf changed description
scarf added
 
question
label
scarf set assignee to scarf
scarf changed assignee from scarf to not set
scarf set assignee to scarf
scarf changed assignee from scarf to not set
Expand operation logs

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

Sign in to comment

Status
Assignees
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(3)
9997491 karasukaigan 1664448219 9719256 wjsr 1712210038 10456722 hestudio admin 1655123533
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

Search

344bd9b3 5694891 D2dac590 5694891