gpt4 book ai didi

java - Jav8 : Computing Elapsed time for Timestamp

转载 作者:行者123 更新时间:2023-12-02 10:08:26 30 4
gpt4 key购买 nike

我有下面的代码来获取当前时间戳并计算最后时间戳的耗时(以分钟为单位),我想知道是否可以进一步针对生产进行优化

public static Timestamp getTimestamp() {
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;
}

public static long getLastTimestampElapse(java.sql.Timestamp oldTime){
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}

最佳答案

从 Java 8 开始,您可以使用 Duration类:

Timestamp timestamp = ...;
Duration duration = Duration.between(timestamp.toInstant(), Instant.now());
long diffMinutes = duration.toMinutes();
System.out.println(diffMinutes);

关于java - Jav8 : Computing Elapsed time for Timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55169515/

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