0

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 ?

Logic
  • 2,230
  • 2
  • 24
  • 41
  • Please try new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") instead of new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"). JSON schema does not like the Z at the end of the date-time – Mana S Nov 30 '21 at 01:04
  • I am unable to parse `2021-11-30T00:22:11.454Z` using the format `yyyy-MM-dd'T'HH:mm:ss.SSSZ` – Logic Nov 30 '21 at 01:09
  • I recommend that you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Exactly how to do instead I cannot tell since I don’t know your `ImmutableDetails` and its builder. But do look into [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Nov 30 '21 at 03:49
  • A [mre], please? – Ole V.V. Nov 30 '21 at 04:03

1 Answers1

1

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.

Ether
  • 53,118
  • 13
  • 86
  • 159
  • My `Details` Class has field `createdAt` of type `Date`. If I remove the conversion I cannot pass String to Details Builder. – Logic Nov 30 '21 at 06:31
  • @Logic I recommend you do not use `Date`. That class too is poorly designed and long outdated. Use `Instant` from java.time, the modern Java date and time API. If that requires passing an `Instant` to your builder, do so: `.createdAt(Instant.parse("2021-11-30T00:22:11.454Z"))`. Yes, it is this simple. – Ole V.V. Nov 30 '21 at 07:03