2

I have a Flutter app that needs to send a post request to my Spring Boot app. The object sent must have a Date in a json string format compatible with the ZonedDateTime java class. The pattern should be: yyyy-MM-ddTHH:mm[:ss[.S]]ZZ[{ZoneId}] An example of a date formatted correctly:

{
   'date':'2007-12-03T10:15:30+01:00[Europe/Paris]'
}

I'm tried to use time_machine 0.9.9 library at pub.dev, but it doesn't generate the correct pattern. Here is an example:

import 'package:time_machine/time_machine.dart';

Map<String, dynamic> toJson() => <String, dynamic>{
    'date': '${Instant.now().inLocalZone()}'
}

Generates:

{
    "date":"2019-08-07T22:24:54 UTC (+00)"
}

Spring boot app throws an exception:

org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2019-08-07T22:24:54 UTC (+00)": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '2019-08-07T22:24:54 UTC (+00)' could not be parsed at index 19

J. Gatica
  • 153
  • 1
  • 10

1 Answers1

2

If you use just Instant.now() without .inLocalZone(), you will get like 2019-08-07T22:24:54Z, which should be OK for your Spring Boot app.

2019-08-07T22:24:54Z is ISO 8601 format, which is the default format for modern Java date and time classes in the java.time API. So a Java ZonedDateTime parses it nicely. The error message said that ZonedDateTime was unable to parse the string that you got from inLocalZone(), quoted as 2019-08-07T22:24:54 UTC (+00).

This was posted as a guess in a comment at first, but another user confirmed that it worked for him/her, after which I posted this answer.

Link. Wikipedia article: ISO 8601

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161