Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在分析ZoneDateTime.parse忽略偏移量_Java_Date_Zoneddatetime_Java Time - Fatal编程技术网

Java 正在分析ZoneDateTime.parse忽略偏移量

Java 正在分析ZoneDateTime.parse忽略偏移量,java,date,zoneddatetime,java-time,Java,Date,Zoneddatetime,Java Time,我正在尝试使用格式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

我正在尝试使用格式
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)));

}

请注意,无论偏移量是多少,结果都是相同的。

在您的字符串中,我将
UTC+01
视为距UTC+1小时的偏移量。因此,虽然UTC可能被解释为时区(实际上并非如此),但这与此处无关,因为字符串中的时间不是UTC,而是UTC+01:00。所以我们不应该使用时区名称
z
,来解析它。这样做基本上是给了你错误的结果(结合解析成
zoneDateTime

@VGR的评论是正确的:我们想要
'UTC'x
。我只举了两个例子,足以证明它们给出了不同的结果

    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss 'UTC'x");

    String str = "22/04/17 09:24:28 UTC+01";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));
    str = "22/04/17 09:24:28 UTC+07";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));
输出为:

UTC前后的单引号表示文本,因此格式化程序检查文本是否存在,但不赋予其任何意义。一个
x
是仅由小时组成的偏移量(除非非零分钟是偏移量的一部分),例如
+01
+12
。由于字符串包含偏移量而不包含时区(例如,英国时间为欧洲/伦敦),
OffsetDateTime
是表示日期和时间的(最)正确类型

退一步说,虽然您的格式完全是人类可读的,但它是非标准的,不适合由机器进行解析。您可能需要考虑是否可以说服字符串的发送者提供ISO 8601格式,例如“代码> 2017—04-22T09:24:28 +01:00 < /代码> ./P>
链接:

@SeanBright如果我这样做,它根本不起作用<代码>线程“main”java.time.format.DateTimeParseException中的异常:无法在索引18处解析文本“22/04/17 09:24:28 UTC+01”
zX
当然不匹配
UTC+01
z
匹配时区名称,如
PST
Pacific Standard time
<代码>UTC+不是时区名称
X
匹配时区偏移量,但我相信不是以
+01
的格式。
O
格式似乎想要
UTC+01:00
而不是
UTC+01
…我怀疑您想要
'UTC'x
z
匹配UTC和
x
匹配
+01
。当解析为
ZoneDateTime
时,它选择区域UTC,并忽略偏移量+01。我猜解析成
OffsetDateTime
是另一种方式。将
UTC+%02d
更改为
+%02d
,然后使用
x
而不是
'UTC'x
在这一点上不是一回事吗?
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss 'UTC'x");

    String str = "22/04/17 09:24:28 UTC+01";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));
    str = "22/04/17 09:24:28 UTC+07";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));
Parsed String "22/04/17 09:24:28 UTC+01", got result of "2017-04-22T09:24:28+01:00"
Parsed String "22/04/17 09:24:28 UTC+07", got result of "2017-04-22T09:24:28+07:00"