1

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.

Fernando Barbeiro
  • 762
  • 4
  • 15
  • 33

1 Answers1

3

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.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • 1
    I wonder if [updating Java’s time zone database](https://www.oracle.com/technetwork/java/javase/documentation/tzupdater-readme-136440.html) would suffice (rather than upgrading to a newer Java version completely). – Ole V.V. Aug 21 '19 at 16:37
  • 2
    @OleV.V. Yes, it will. All that data at run-time comes from `tzdata`, which is what that tool refers to updating. Wasn't aware that this tool existed though, I've never needed to use it. – Michael Aug 21 '19 at 16:41