在 Java8 中使用时区格式化 LocalDateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25561377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:39:55  来源:igfitidea点击:

Format LocalDateTime with Timezone in Java8

javajava-8java-time

提问by carfield

I have the this simple code:

我有这个简单的代码:

DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z");
LocalDateTime.now().format(FORMATTER)

Then I will get following exception:

然后我会得到以下异常:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3315)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1745)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1719)
at java.time.LocalDateTime.format(LocalDateTime.java:1746)

How to resolve this issue?

如何解决这个问题?

采纳答案by ntalbs

LocalDateTimeis a date-time without a time-zone. You specified the time zone offset format symbol in the format, however, LocalDateTimedoesn't have such information. That's why the error occured.

LocalDateTime是没有时区的日期时间。您在格式中指定了时区偏移格式符号,但是,LocalDateTime没有此类信息。这就是错误发生的原因。

If you want time-zone information, you should use ZonedDateTime.

如果你想要时区信息,你应该使用ZonedDateTime.

DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z");
ZonedDateTime.now().format(FORMATTER);
=> "20140829 14:12:22.122000 +09"

回答by Meno Hochschild

The prefix "Local" in JSR-310 (aka java.time-package in Java-8) does not indicate that there is a timezone information in internal state of that class (here: LocalDateTime). Despite the often misleading name such classes like LocalDateTimeor LocalTimehave NO timezone information or offset.

JSR-310 中的前缀“Local”(又名 Java-8 中的 java.time-package)并不表示该类的内部状态中存在时区信息(此处:)LocalDateTime。尽管这些类通常具有误导性的名称,例如LocalDateTimeLocalTime没有时区信息或偏移量

You tried to format such a temporal type (which does not contain any offset) with offset information (indicated by pattern symbol Z). So the formatter tries to access an unavailable information and has to throw the exception you observed.

您尝试使用偏移信息(由模式符号 Z 表示)格式化此类时间类型(不包含任何偏移)。因此格式化程序尝试访问不可用的信息,并且必须抛出您观察到的异常。

Solution:

解决方案:

Use a type which has such an offset or timezone information. In JSR-310 this is either OffsetDateTime(which contains an offset but not a timezone including DST-rules) or ZonedDateTime. You can watch out all supported fields of such a type by look-up on the method isSupported(TemporalField).. The field OffsetSecondsis supported in OffsetDateTimeand ZonedDateTime, but not in LocalDateTime.

使用具有此类偏移或时区信息的类型。在 JSR-310 中,这是OffsetDateTime(包含偏移量但不包含包含 DST 规则的时区)或ZonedDateTime. 您可以通过在isSupported(TemporalField)方法上查找来查看所有支持的此类类型的字段. OffsetSecondsOffsetDateTime和 中支持该字段ZonedDateTime,但在 中不支持LocalDateTime

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z");
String s = ZonedDateTime.now().format(formatter);

回答by karthik r

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"));

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"));