我使用Hibernate 5& MySQL。
这就是保存到数据库中的内容:2018-03-11 06:26:47.336 --我不认为这是24小时格式,但是如何查看AM/PM呢?如何以24小时格式节省时间?
运行SELECT @@global.time_zone; in MySQL向我展示了:+00:00,所以我认为我已经准备好接受UTC的时间了?这就是我如何设置pojo字段来设置时间的方法:
Clock clock = Clock.systemUTC();
LocalDateTime userCreated = LocalDateTime.now(clock);它接受LocalDateTime。但是,当我查询时,从数据库中得到的是:u1.getUserCreated(): 2018-03-11T01:26:47.336,当我试图将时间转换为特定于区域的时间时,我得到以下内容:
ZonedDateTime z1 = ZonedDateTime.of(u1.getUserCreated(), ZoneId.of("America/New_York"));
System.out.println("z1: " + z1);
// z1: 2018-03-11T01:26:47.336-05:00[America/New_York]但实际上应该是:9:26:47.336 PM (21:26:47.336),正如您在这个站点上看到的:http://www.timebie.com/std/utc.php
发布于 2018-03-11 22:45:02
你只是没有正确地转换。您的LocalDateTime表示UTC时区中的挂钟时间。不在纽约时区。因此yo需要在UTC中将其转换为ZonedDateTime::
ZonedDateTime utcDateTime = u1.getUserCreated().atZone(ZoneOffset.UTC);然后,如果您想在同一时间内获得一个ZonedDateTime,但是在纽约时区,那么,只需这样做:
ZonedDateTime newYorkDateTime = utcDateTime.withZoneSameInstant(ZoneId.of("America/New_York"));https://stackoverflow.com/questions/49225581
复制相似问题