tuk*_*tuk 10 java amazon-web-services java-11 java-17
我们将从 Java 11 (correto11) 迁移到 Java 17 (correto17)。作为其中的一部分,我们还必须将 Ubuntu 升级到standard:6.0AWSstandard:4.0中,如此处所述。
我们观察到 Java 11 和 Java 17 中的Instant.now()输出有点不同。
例如,
System.out.println("Instant: " + Instant.now());
Run Code Online (Sandbox Code Playgroud)
给出如下输出
Instant: 2022-12-12T18:04:27.267229ZInstant: 2022-12-12T18:04:27.267229114Z有人可以让我知道是什么原因造成的吗?我怎样才能使这两种情况下的行为相同?
Arv*_*ash 11
Instant#now()从系统时钟获取当前时刻。如果系统时钟返回的精度仅达到微秒,则Instant#toString只需截断九位数字中的最后三个零。如果运行 Java 17 (correto17) 的系统返回纳秒精度,您可以使用 将其截断为微秒精度Instant#truncatedTo(java.time.temporal.TemporalUnit)。
public class Main {
public static void main(String args[]) {
Instant instant = Instant.now();
Instant truncatedToMicros = instant.truncatedTo(ChronoUnit.MICROS);
System.out.println(truncatedToMicros);
}
}
Run Code Online (Sandbox Code Playgroud)
从Trail: Date Time中了解有关现代日期时间 API 的更多信息。
| 归档时间: |
|
| 查看次数: |
5611 次 |
| 最近记录: |