【发布时间】:2020-07-01 12:43:40
【问题描述】:
我的代码这样做是为了显示当前日期和时间
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String formattedDate = dtf.format(LocalDateTime.now());
因为无论如何我都使用 GMT,所以我没有注意到它总是使用 GMT 时区显示,这是客户在不同时区报告的。但我很困惑,因为我认为 LocalDateTime 是 local 日期时间?
【问题讨论】:
-
时区显示从何而来?您的日期格式化程序
dtf是否包含时区? -
不,只是更新的问题,时区应该在格式化程序中还是在 dateTime 中?
-
因为显示时间错误
-
“我以为 LocalDateTime 是本地日期时间” --- 是的!它是 JVM 的 默认时区本地的,就像 documentation 说的,即
now()的 javadoc:从系统获取当前日期时间时钟在默认时区。