public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
Instant instant = Instant.from(zdt);
Timestamp timestamp = Timestamp.from(instant);
System.out.println(ldt + "\n");
System.out.println(zdt + "\n");
System.out.println(instant + "\n");
System.out.println(timestamp + "\n");
}
它打印:
2017-05-07T18:13:26.969
2017-05-07T18:13:26.969-04:00[America/New_York]
2017-05-07T22:13:26.969Z
2017-05-07 18:13:26.969
如何Timestamp
在与SQL同时保存SQL Instant
?我需要能够Timestamp
从任何地方获取它,并将其转换为世界那部分的任何时间。问题在于,它与我的系统时钟设置的时间保持相同的时间。
最好Timestamp
从a 获取LocalDateTime
而不是从a获取Instant
。
第一步是将您的代码ZonedDateTime
转换为GMT:
ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of("GMT"));
然后,您可以Timestamp
通过将其转换为LocalDateTime
:
Timestamp timestamp = Timestamp.valueOf(gmt.toLocalDateTime());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句