I have a client written in Flutter and a server written in Spring Boot. The client sends the DateTime
object in the format of yyyy-MM-dd HH:mm:ss.SS
. The milliseconds part can change from ".SS" to ".SSSS", sometimes to ".SSS". So there is no stable format for the milliseconds part. The server accepts LocalDateTime
object. How can I reconcile both sides with one DateTime format? Actually, I want the server accepts all kind of DateTime objects without looking at the milliseconds part because I don't need the milliseconds part. The accuracy of the DateTime should be up to seconds. What kind of logic should I follow?
I think the best place to handle the format is in the server side dtos
. I have found an annotation that accepts the given DateTime parameter.
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
. However, If the client sends the DateTime in the format of "yyyy-MM-dd HH:mm:ss.SSS" or other than the given format inside the annotation. Java throws the following error.
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-05-30 11:08:32.832509": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-05-30 11:08:32.832509' could not be parsed, unparsed text found at index 23; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-05-30 11:08:32.832509": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-05-30 11:08:32.832509' could not be parsed, unparsed text found at index 23<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 320] (through reference chain: com.napsam.wo.dtos.EventCreateRequest["startingTime"])]
What kind of logic should I follow?