uniwinux
uniwinux

Reputation: 27

DateTimeParseException could not be parsed at index xx

I have a data out of 50k reading line by line, one got a,

s = "2018-11-05T06:14:10.6-05:00".

Which will get error on,

public static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX");
            
if(s!=null && isValidDate(s)) {
   OffsetDateTime oDAtOffset = OffsetDateTime.parse(s, DATE_TIME_FORMATTER); //error!
}

If s = "2018-11-05T06:14:10.006-05:00" then it won't get error.

How do I convert from "2018-11-05T06:14:10.6-05:00" to "2018-11-05T06:14:10.006-05:00"?

Upvotes: 0

Views: 154

Answers (2)

Christoph Dahlen
Christoph Dahlen

Reputation: 836

You can use the ISO 8601 DateTimeFormatter, which handles both variants:

var t = java.time.ZonedDateTime.parse("2018-11-05T06:14:10.6-05:00");
System.out.println(t);

Output:

2018-11-05T06:14:10.600-05:00

As already said: .6 is not equal to .006

Upvotes: 1

uniwinux
uniwinux

Reputation: 27

I just do it like this,

s = s.substring(21,22).equals("-") ? s.substring(0,21)+"00"+s.substring(21,27) : s;

Upvotes: 0

Related Questions