【发布时间】:2019-07-12 13:03:20
【问题描述】:
我正在尝试使用 dd/MM/yy HH:mm:ss zX 格式解析 22/04/17 09:24:28 UTC+01 - 但无论偏移量如何,创建的 ZonedDateTime 都是相同的。
下面是一个重现此问题的示例,我正在以编程方式更改偏移量:
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss zX")
.withZone(ZoneId.systemDefault());
System.out.println(MessageFormat.format("Current time is \"{0}\"", formatter.format(Instant.now())));
for (int i = 1; i <= 12; i++) {
final String str = String.format("22/04/17 09:24:28 UTC+%02d", i);
System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"", str,
ZonedDateTime.parse(str, formatter)));
}
还有输出:
Current time is "12/07/19 12:59:25 ZZ"
Parsed String "22/04/17 09:24:28 UTC+01", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+02", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+03", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+04", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+05", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+06", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+07", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+08", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+09", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+10", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+11", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+12", got result of "2017-04-22T09:24:28Z[UTC]"
请注意,无论偏移量如何,结果都是相同的。
【问题讨论】:
-
@SeanBright 如果我这样做,它根本不起作用。
Exception in thread "main" java.time.format.DateTimeParseException: Text '22/04/17 09:24:28 UTC+01' could not be parsed at index 18 -
zX肯定不匹配UTC+01。z匹配时区名称,例如PST或Pacific Standard Time。UTC+<num>不是时区名称。X匹配时区偏移量,但我相信不是+01格式。O格式似乎需要UTC+01:00而不是UTC+01... -
我怀疑你想要
'UTC'x。 -
z匹配 UTC,X匹配+01。在解析为ZonedDateTime时,它会选择区域UTC,并忽略偏移量+01。我的猜测是解析成OffsetDateTime会反过来。 -
不会将
UTC+%02d更改为+%02d然后使用x而不是'UTC'x在这一点上是一样的吗?
标签: java date zoneddatetime java-time