-2

I am getting below error when trying with pattern with below pattern

private static final DateTimeFormatter DATE_TIME_FORMATTER
        = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");

Can someone explain reason behind this??

Error received with above pattern: java.time.format.DateTimeParseException: Text '2021-08-03T10:00:00EDT' could not be parsed at index 19

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    Which Java version are you using? Also, what's your current `Locale`? – MC Emperor Nov 25 '21 at 12:08
  • [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/a/27479533/4718933) – Simeone Deweixu Nov 25 '21 at 12:20
  • 1
    On my computer this works for both Locales of 'en_US' and 'hi_IN'. 19 puts you at the 'E'. Wonder if your Java installation does not know about EDT, which is Eastern Daylight Time. – ProgrammersBlock Nov 25 '21 at 12:46
  • This code snippet might help. It did not print 'EDT' specifically but did print variations. for (String t : TimeZone.getAvailableIDs()) {System.out.println(t);} – ProgrammersBlock Nov 25 '21 at 12:52
  • A [mre], please? Also asking because I cannot reproduce either. I am parsing your string without any signs of problems. – Ole V.V. Nov 25 '21 at 15:36
  • Your string is funny. It’s trying to be [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) but the trailing `EDT` disagrees with ISO 8601 (ISO 8601 would be like `2021-08-03T10:00-04:00`). You will probably prefer if you can get a real ISO 8601 string. – Ole V.V. Nov 25 '21 at 15:41
  • Could there be a funny non-printing character between the last `00` and `EDT`? – Ole V.V. Nov 25 '21 at 16:04
  • Does this answer your question? [Text could not be parsed, unparsed text found](https://stackoverflow.com/questions/68335396/text-could-not-be-parsed-unparsed-text-found) – Ole V.V. Nov 26 '21 at 12:26
  • @ProgrammersBlock We din’t know whether it’s supposed to mean Australian or North American/Caribbean Eastern Daylight Time. Neither does Java, so if we get a result, we don’t know which one. Parsing time zone abbreviations is a non-deterministic business. – Ole V.V. Nov 26 '21 at 12:43

1 Answers1

0

You can parse by following way

String val="2021-08-03T10:00:00EDT";
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
ZonedDateTime zonedDateTime=ZonedDateTime.parse(val,DATE_TIME_FORMATTER);
System.out.println(zonedDateTime);
mystery
  • 875
  • 6
  • 16