gpt4 book ai didi

java - 由于静态问题,时间不会以动态形式返回

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:43 25 4
gpt4 key购买 nike

我需要通过我的服务类显示最新时间戳:

@Service
public class UserServiceImpl implements UserService {

private static final AtomicInteger counter = new AtomicInteger();
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
static Timestamp timestamp = new Timestamp(System.currentTimeMillis());

static List<User> users = new ArrayList<User>(
Arrays.asList(
new User("123","abc","A1234567", "a529794b3ea9c05d8f0b5f354dac25e5", "Role-A","Screen A","Execute",sdf.format(timestamp) )));

@Override
public List<User> getAll(int offset, int count) {
return users;
}

输出如下:

[{"hkid":"123","username":"abc","passportNo":"A1234567","tokenId":"a529794b3ea9c05d8f0b5f354dac25e5","role":"Role-A","permission":"Execute","timestamp":"2018.04.02.12.16.19","screen":"Screen A"}]

现在的问题是时间戳不会作为当前时间戳返回,因为我的时间戳文件是静态的。我需要进行哪些必要的更改才能始终获得当前时间戳。我必须使用静态对象。

补充一下:在 pojo 中,我将时间戳记为字符串

public class User implements Serializable {

private String id;
private String username;
private String passportNo;
private String tokenId;
private String role;
private String Screen ;
private String permission;
private String timestamp ;

最佳答案

您可以在每次调用 getAll 时动态更新它:

@Override
public List<User> getAll(int offset, int count) {
String ts = sdf.format(new Timestamp(System.currentTimeMillis()));
users.forEach(u -> u.setTimestamp(ts));
return users;
}

关于java - 由于静态问题,时间不会以动态形式返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com