javajsondatejsonschemajsonschema2pojo

Date object in JSON schema


I have my json schema for createdAt as

{
  "title": "details",
  "definitions": {
    "dateTime": {
      "type": "string",
      "format": "date-time"
    },
  "properties" : {
    "createdAt": { "$ref": "#/definitions/dateTime" }
  }
}

and when I try to create Object

Details details = ImmutableDetails.builder()
         .createdAt(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
           .parse("2021-11-30T00:22:11.454Z"))
         .build(); 

I am getting the error as

createDate: [Nov 30, 2021, 0:22:11 AM] is not a valid date-time. 
Expected [yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}Z, 
          yyyy-MM-dd'T'HH:mm:ss[+-]HH:mm, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,9}[+-]HH:mm] 

How do I specify my JSON schema to accept java.util.Date object ?


Solution

  • The "date-time" format in JSON Schema follows RFC3339. That's timestamps that look like 2021-12-31T23:59:59Z or 2021-12-31T23:59:59-05:30, which is exactly what the error message says.

    You're already starting out with timestamps in the correct format, and then converting them to an unacceptable format -- so just remove that part.