DateTimeParseException: Text '2020-04-01T08:53:47.000+02:00 00:00' could not be parsed, unparsed text found at index 29

383 Views Asked by At

Getting a DateTimeParseExcpetion when trying to convert the String 2020-04-01T08:53:47.000+02:00 00:00

String date = "2020-04-01T08:53:47.000+02:00 00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
parsedDate = LocalDateTime.parse(date,formatter).toString();
System.out.println(parsedDate);
3

There are 3 best solutions below

0
On BEST ANSWER

Your pattern is not the same as your String. Check the last part where is 000+02:00 00:00.

Your pattern is: SSSz

If you try this:

String date = "2020-04-01T08:53:47.000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

It will works because your date is like your pattern. Note that every number in the date is into pattern too.

But for your date there is an empty space what no make sense, so removing it, the code works perfectly.

String date = "2020-04-01T08:53:47.000+02:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

Output:

2020-04-01T08:53:47

Note that z is the local time and means "zero hour offset" or "Zulu time" (UTC) and you can use Locale.

0
On

The 00:00 at the end of your date-time string doesn't make sense to me. Parse the date-time string after stripping that.

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDate = "2020-04-01T08:53:47.000+02:00 00:00";
        strDate = strDate.substring(0, strDate.lastIndexOf(' '));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);
        LocalDateTime parsedDate = LocalDateTime.parse(strDate, formatter);
        System.out.println(parsedDate);

        OffsetDateTime odt = OffsetDateTime.parse(strDate);
        System.out.println(odt);
        System.out.println(odt.getOffset());
    }
}

Output:

2020-04-01T08:53:47
2020-04-01T08:53:47+02:00
+02:00

Note: You can parse your date-time string (after striping 00:00 from the end of it) to OffsetDateTime in order to preserve the zone-offset information.

0
On

Use the built-in formatter

The built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME matches the part of your string that we can understand. And it can parse just that part and ignore the rest.

    String date = "2020-04-01T08:53:47.000+02:00 00:00";
    ParsePosition pp = new ParsePosition(0);
    OffsetDateTime odt = OffsetDateTime.from(
            DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(date, pp));
    
    System.out.println("Date and time: " + odt);
    System.out.println("Unparsed text: " + date.substring(pp.getIndex()));

Output:

Date and time: 2020-04-01T08:53:47+02:00
Unparsed text:  00:00

Since your string contains an offset from UTC, OffsetDateTime is the correct class to parse into. If we used LocalDateTIme, the offset would be ignored, and we would end up not knowing at which offset the time was to be interpreted, that is, we could not know which point in time it was. With OffsetDateTime the point in time is unambiguous. If you want to convert to the time in your own time zone, convert to ZonedDateTime (still not LocalDateTime).

    ZonedDateTime timeInMyTimeZone = odt.atZoneSameInstant(ZoneId.systemDefault());
    System.out.println("Date and time: " + timeInMyTimeZone);

Example output:

Date and time: 2020-04-01T11:53:47+05:00[Asia/Aqtobe]

Links

Documentation links: