【发布时间】:2022-02-02 02:25:46
【问题描述】:
我正在尝试使用 ZonedDateTime.ofInstant 获取时间,给出一个特定的日期时间,但是我注意到在某些情况下给出的结果并没有包含秒数。 代表我正在处理的情况的代码示例
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a long variable for milliseconds
//this is the same as "2022-01-31T00:00:00.00Z"
long milliseconds1 = 1643587200000L;
long milliseconds2 = 1643587201000L;
// get Instant using ofEpochMilli() method
Instant instant1
= Instant.ofEpochMilli(milliseconds1);
// create Instant object
Instant instant2
= Instant.ofEpochMilli(milliseconds2);
// create a ZonID
ZoneId zone = ZoneId.of("Europe/Lisbon");
// apply ofInstant method
// of ZonedDateTime class
ZonedDateTime zt1 = ZonedDateTime
.ofInstant(instant1, zone);
ZonedDateTime zt2 = ZonedDateTime
.ofInstant(instant2, zone);
// print the result
System.out.println("ZonedDateTime is " + zt1);
System.out.println("ZonedDateTime is " + zt2);
}
}
这段代码执行的结果如下:
ZonedDateTime is 2022-01-31T00:00Z[Europe/Lisbon]
ZonedDateTime is 2022-01-31T00:00:01Z[Europe/Lisbon]
我需要获取第一个结果中未显示的秒数,但我不明白为什么它没有显示 ZonedDateTime.ofInstant 是否有理由不提供完整时间,或者是否有其他方法可以获取 YYYY-MM-DD:HH:MM:SS 中特定时区的时间?
【问题讨论】:
-
这能回答你的问题吗? Parsing timestamp as LocalDateTime