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
如何使SQL Timestamp
与Instant
保存相同的时间?我需要能够从任何地方获取Timestamp
并将其转换为当地的时间。问题在于,它始终保存为与我的系统时钟设置相同的时间。
DATE
列时,以下代码对我很有用。Timestamp.valueOf(ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneOffset.UTC).toLocalDateTime())
- AlikElzin-kilaka