Veer7
Veer7

Reputation: 21513

java.time.format.DateTimeParseException: Text '2016-08-30T06:18:17:698-0600' could not be parsed at index 24

Here is my java8 code for this date string "2016-08-30T06:18:17:698-0600"

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD'T'HH:mm:ss:SSS'-'XXX");
    String attDate = "2016-08-30T06:18:17:698-0600";
    //attDate = attDate.substring(0, 19);
    System.out.println("####attask date: "+attDate);
    LocalDateTime dt = LocalDateTime.parse(attDate, formatter);
    System.out.println(dt);

Using LocalDate with truncated date string will solve this issue but I can't simply remove Time and use LocalDate instead of LocalDateTime

Please suggest what's going wrong here. I also need to why your answer will work.

Upvotes: 0

Views: 2009

Answers (2)

MarcosCordeiro
MarcosCordeiro

Reputation: 93

If you change your pattern to yyyy-MM-dd'T'HH:mm:ss.SSSZ it will print 2016-08-30T06:18:17.123.

But in your code, the date are with : in milliseconds when the correct is ..

Complete code:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        String attDate = "2016-08-30T06:18:17.123-0600";
        //attDate = attDate.substring(0, 19);
        System.out.println("####attask date: "+attDate);
        LocalDateTime dt = LocalDateTime.parse(attDate, formatter);
        System.out.println(dt);

Output:

####attask date: 2016-08-30T06:18:17.123-0600
2016-08-30T06:18:17.123

Upvotes: 2

Affe
Affe

Reputation: 47994

The minus in the zone offset is part of the zone offset expression, you should not be escaping it as a literal.

Upvotes: 4

Related Questions