java.time.format.DateTimeParseException: Text could not be parsed at index 21

We Are Going To Discuss About java.time.format.DateTimeParseException: Text could not be parsed at index 21. So lets Start this Java Article.

java.time.format.DateTimeParseException: Text could not be parsed at index 21

  1. java.time.format.DateTimeParseException: Text could not be parsed at index 21

    If your input always has a time zone of “zulu” (“Z” = UTC), then you can use DateTimeFormatter.ISO_INSTANT (implicitly):
    final Instant parsed = Instant.parse(dateTime);

  2. java.time.format.DateTimeParseException: Text could not be parsed at index 21

    If your input always has a time zone of “zulu” (“Z” = UTC), then you can use DateTimeFormatter.ISO_INSTANT (implicitly):
    final Instant parsed = Instant.parse(dateTime);

Solution 1

The default parser can parse your input. So you don’t need a custom formatter and

String dateTime = "2012-02-22T02:06:58.147Z";
ZonedDateTime d = ZonedDateTime.parse(dateTime);

works as expected.

Original Author assylias Of This Content

Solution 2

If your input always has a time zone of “zulu” (“Z” = UTC), then you can use DateTimeFormatter.ISO_INSTANT (implicitly):

final Instant parsed = Instant.parse(dateTime);

If time zone varies and has the form of “+01:00” or “+01:00:00” (when not “Z”), then you can use DateTimeFormatter.ISO_OFFSET_DATE_TIME:

DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter);

If neither is the case, you can construct a DateTimeFormatter in the same manner as DateTimeFormatter.ISO_OFFSET_DATE_TIME is constructed.


Your current pattern has several problems:

  • not using strict mode (ResolverStyle.STRICT);
  • using yyyy instead of uuuu (yyyy will not work in strict mode);
  • using 12-hour hh instead of 24-hour HH;
  • using only one digit S for fractional seconds, but input has three.

Original Author Roman Of This Content

Solution 3

Your original problem was wrong pattern symbol “h” which stands for the clock hour (range 1-12). In this case, the am-pm-information is missing. Better, use the pattern symbol “H” instead (hour of day in range 0-23). So the pattern should rather have been like:

uuuu-MM-dd’T’HH:mm:ss.SSSX (best pattern also suitable for strict mode)

Original Author Meno Hochschild Of This Content

Solution 4

The following worked for me

import java.time.*;
import java.time.format.*;

public class Times {

  public static void main(String[] args) {
    final String dateTime = "2012-02-22T02:06:58.147Z";
    DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
    final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter.withZone(ZoneId.of("UTC")));
    System.out.println(parsed.toLocalDateTime());
  }
}

and gave me output as

2012-02-22T02:06:58.147

Original Author Meno Hochschild Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment