-1

I am having trouble parsing the following date into a LocalDate object.

2017-11-10T14:17:38.611+0000

I tried the following:

LocalDate date = LocalDate.parse(markitDate, DateTimeFormatter.BASIC_ISO_DATE)

but, I get an error that the letter T cannot be parsed.

kgui
  • 4,015
  • 5
  • 41
  • 53
  • 1
    Check the [DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) first. `ISO_OFFSET_DATE_TIME`. It use `Z zone-offset offset-Z +0000; -0800; -08:00;` – AxelH Nov 10 '17 at 15:01
  • 1
    I get an error saying it index 23 cannot be parsed. index 23 is the + character. – kgui Nov 10 '17 at 15:08

3 Answers3

1

The way to do this explicitly, maybe there's a defined pattern out there already but, the following worked:

LocalDateTime dateTime = LocalDateTime.parse(markitDate, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
kgui
  • 4,015
  • 5
  • 41
  • 53
0

According to sources of DateTimeFormatter.BASIC_ISO_DATE:

 * The ISO date formatter that formats or parses a date without an
 * offset, such as '20111203'

and you trying to parse date and time

WeGa
  • 801
  • 4
  • 10
  • 24
0

Because it is not a BASIC_ISO_DATE.

Take a look at the other predefined formatters and you will find that ISO_OFFSET_DATE_TIME is the right choice here. Use it like this:

LocalDate date = LocalDate.parse(markitDate, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
f1sh
  • 11,489
  • 3
  • 25
  • 51