LocalDateTime API允许通过使用格式化程序中的键“ z”来添加TimeZone名称。添加此密钥时出现异常,不明白为什么。我正在寻找类似此示例的内容11:59:22 PM GMT,而不是“ **... UMT+2**”。
我的代码:
public class TimeZone
{
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a z");
System.out.println(now.format(formatter));
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime
at java.time.format.DateTimePrintContext.getValue(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatter.formatTo(Unknown Source)
at java.time.format.DateTimeFormatter.format(Unknown Source)
at java.time.LocalDateTime.format(Unknown Source)
at tz.TimeZone.main(TimeZone.java:12)
Run Code Online (Sandbox Code Playgroud)
这是DateTimeFormatter API部分:
All letters 'A' to 'Z' and 'a' to 'z' are reserved as …Run Code Online (Sandbox Code Playgroud)