-1

I used DateTimeFormatter.ofPattern('yyyy-MM-dd'T'HH:mm:ssZ') and I get error like this

java.time.format.DateTimeParseException: Text '2020-04-13T12:05:54+0600' could not be parsed at index 19

String which i wanna parse is '2020-04-13T12:05:54+0600'

so how can i solve this? What pattern i need to use?

  • Does this answer your question? [Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10](https://stackoverflow.com/questions/39033525/error-java-time-format-datetimeparseexception-could-not-be-parsed-unparsed-tex) – Vishwa Ratna Apr 13 '20 at 06:20
  • 1
    Please provide a [mcve]. At the moment We can't tell what you're doing with the pattern. – Jon Skeet Apr 13 '20 at 06:35
  • I can’t reproduce. `DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").parse("2020-04-13T12:05:54+0600")` does not throw any exception for me (you are probably aware that you need double quotes around the strings, not single quotes). Also `OffsetDateTime.parse("2020-04-13T12:05:54+0600", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"))` works. – Ole V.V. Apr 13 '20 at 07:01

2 Answers2

0

You can pass pattern of Like this way.

DateTimeFormatter.ofPattern("yyyy MM dd");

Please follow the documentation to pass proper pattern.

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Sanjay
  • 2,265
  • 1
  • 11
  • 26
  • A link-only answer should be posted as a comment, not as an answer, please. I acknowledge that the link could be useful. – Ole V.V. Apr 13 '20 at 07:07
0

Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.

So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.

So you need to use a DateTimeFormatter to specify the format of your input string. Something like:

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");

LocalDateTime result = LocalDateTime.parse(convertDate, format);

Answer copied from greg449’s answer here

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
  • 2
    You must link the source from where you copied the answer. In this case: https://stackoverflow.com/questions/39033525/error-java-time-format-datetimeparseexception-could-not-be-parsed-unparsed-tex – Vishwa Ratna Apr 13 '20 at 06:19
  • sorry about this. i am new here. i will take care of this. – anuj padmawar Apr 13 '20 at 07:11