1

My POJO has a variable annotated like this

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
@JsonDeserialize(using = ZonedDateTimeDeserializer.class)
private ZonedDateTime startTs;

//other private variables

My goal is to map this to an Entity for which I'm using ModelMapper and the sample code goes like this

public POJOEntity mapToPOJOEntity(POJO pojo) {
    return modelMapper.map(pojo, POJOEntity.class);
}

After mapping when I look at the startTs variable in debug mode it has the desired format but when I check in the db startTs is saving as a whole ZonedDateTime object rather than the desired format I specified in POJO. Did anyone face this issue if yes can someone please help? Value saved in cosmosDB: Value in cosmosDB

Expected output: "startTs": "2023-01-12T08:58:32.452-06:00"

PS: CosmosDB, spring-boot, Java8, ModelMapper version: 2.3.8

sai23
  • 65
  • 1
  • 8
  • Filed has data type ‘ZonedDateTime’.‘ Not sure why do you expect to see ‘String’ there? – Alex Jan 12 '23 at 16:07
  • ‘ @JsonFormat’ should take effect when you serialize your POJO to json and mapper just map one object to another – Alex Jan 12 '23 at 16:09
  • Since I'm using @JsonFormat after serializing and saving it to the db it should save in this format `"2023-01-12T08:58:32.452-06:00"` since that is the pattern I'm using but it is saving the whole object as shown in image. – sai23 Jan 12 '23 at 17:02
  • not sure how cosmosDB serializes it but your question was about ModelMapper. To validate your annotations you can create test using Jackson ObjectMapper to serialize explicitly and check results – Alex Jan 12 '23 at 20:03
  • I did not find anything online to fix this. I went with converting ZonedDateTime to String and saving it to the CosmosDB. – sai23 Jan 17 '23 at 15:29
  • In .net using `NodaTime` package, we are able to store datetime in desired format on Cosmos DB. For Java the alternative of the `NodaTime` package is `Joda Time` Here are some references. [JodaTime](https://nodatime.org/3.1.x/userguide/rationale) [Geeksforgeeks](https://www.geeksforgeeks.org/joda-time/) [StackOverflow](https://stackoverflow.com/questions/14331063/how-to-get-properly-current-date-and-time-in-joda-time) – RitikaNalwaya Jan 19 '23 at 05:53

0 Answers0