Fernando Barbeiro
Fernando Barbeiro

Reputation: 812

DateTimeParseException while trying to perform ZonedDateTime.parse

Using Java 8u222, I've been trying a silly operation and it incurs in an error that I'm not being able to fully understand. The line code:

ZonedDateTime.parse("2011-07-03T02:20:46+06:00[Asia/Qostanay]");

The error:

java.time.format.DateTimeParseException: Text '2011-07-03T02:20:46+06:00[Asia/Qostanay]' could not be parsed, unparsed text found at index 25
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:582)

Using the same date (although the timezone could be incorrect, the intention is just testing here), I changed the square bracket's value and it works, I mean:

ZonedDateTime.parse("2011-07-03T02:20:46+06:00[Europe/Busingen]);

It works as expected, as well as other values such:

ZonedDateTime.parse("2011-07-03T02:20:46+06:00[Asia/Ulan_Bator]")
ZonedDateTime.parse("2011-07-03T02:20:46+06:00[SystemV/CST6CDT]")

I found some similar questions such as the one below, but not precisely the same usage that I'm trying / facing. Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

Does someone have an understanding of Java Date API to help me out to grasp what I'm doing wrong here?

Thanks.

Upvotes: 1

Views: 144

Answers (1)

Michael
Michael

Reputation: 44240

Asia/Qostanay is a zone which doesn't exist in the JDK8's list of timezones. It was added later.

If you don't care about the location of the timezone then just splice the [...] part of the string off the end before parsing. Knowing that the time is +06:00 is going to sufficient for almost all purposes.

Alternatively, upgrade to a more recent version of Java.

Upvotes: 3

Related Questions