-2

I am converting the date "2019-12-17T06:50:00.000Z" to OffsetDateTime like

OffsetDateTime.parse("2019-12-17T06:50:00.000Z"). 

I am getting error as

java.time.format.DateTimeParseException: Text '[{"date":"2019-12-17T02:10:00.000Z"}]' could not be parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949).

How can parse this?

shahaf
  • 4,750
  • 2
  • 29
  • 32
Vdev
  • 319
  • 1
  • 2
  • 11

1 Answers1

2

You are attempting to parse the JSON [{"date":"2019-12-17T02:10:00.000Z"}] with OffsetDateTime however you should only parse the date field from the JSON. Take a look at How to parse JSON in Java question to understand how to extract a field from JSON.

If this is a very simple case where you always have the same input format you can use String.substring()

String json = "[{\"date\":\"2019-12-17T02:10:00.000Z\"}]";
String value = json.substring(10, 34);
OffsetDateTime dt = OffsetDateTime.parse(value);
System.out.println(dt); // 2019-12-17T02:10Z
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Hi Karol, I have scenario where i am getting an instant converted to OffsetDateTime, then i need to do like ChronoUnit.HOURS.between(Instant.parse(offsetDateTime), Instant.now()) It give me DateTimeParseException in case of 2021-07-15T05:27Z as if it was 2021-07-15T05:27:00Z . it would work fine – Biyu Jul 21 '21 at 13:41