-1

I send some request with Deferred Date to a server. The POST request looks like:

{
   "text":"texst",
   "deferred_date":"2019-09-14 14:00"
}

I send it via Postman or the Unirest in Java and have a 201 Accepted result. The server stores this date value in "2019-09-14T14:00:00+03:00" format.

Since I modify my Date in Java, the final Data is stored in a String

YYYY-MM-dd hh:mm format

DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm");
String deferredTime = "";
deferredTime = dateFormat.format(oldTime);

My original Unirest's POST has the next body construction:

    .body("{\"text\":\"...\",\"deferred_date\":\"2019-09-14 06:03\"}")

and it works fine, but if I use my deferredTime String in my POST:

    .body("{\"text\":\"...\",\"deferred_date\":\"" + deferredTime + "\"}")

I receive a 400 Bad Request error

{
   "userMessage":"Invalid input.",
   "errorCode":"invalid",
   "fields":{
      "deferred_date":[
         "Posting date Date has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
      ]
   },
   "internalMessage":"Invalid input."
}

I don't understand why it doesn't work if the deferredTime String variable keeps the same "YYYY-MM-dd hh:mm" value as I used before. Any suggestions?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Evgenii Truuts
  • 349
  • 1
  • 5
  • 17
  • 1
    Print the `deferredTime` and check if its format is same as the previous one – Phenomenal One Sep 14 '18 at 16:54
  • @MaruthiAdithya Of course I did it and it printed correctly in needed format – Evgenii Truuts Sep 14 '18 at 16:56
  • 1
    The Error tells that the server expects Date in `ISO8601 format` Try this `DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm");` – Phenomenal One Sep 14 '18 at 16:59
  • @MaruthiAdithya I tried it but received the same error. – Evgenii Truuts Sep 14 '18 at 17:36
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). You’ve got a couple of case errors in your format pattern string, you need lowercase `yyyy` and uppercase `HH`, but I can’t see how that alone would cause the bad request error. – Ole V.V. Sep 14 '18 at 19:04
  • @OleV.V. Thank you, it works. I used LocalDateTime and solved my problem with just several strings. Thank you! – Evgenii Truuts Sep 16 '18 at 07:42
  • 1
    @OleV.V. the LocalDateTime String is stored my Date in "2018-09-16T10:41:48.619" format instead of SimpleDateFormat which is stored the Date in "2018-09-16 10:41". Now, with a correct format, I receive a 201 Accepted result – Evgenii Truuts Sep 16 '18 at 07:50

1 Answers1

1

In accordance with Ole V.V answer, I used the LocalDateTime String is stored my Date in "2018-09-16T10:41:48.619" format instead of SimpleDateFormat which is stored the Date in "2018-09-16 10:41". Now, with a correct format, I receive a 201 Accepted result

Evgenii Truuts
  • 349
  • 1
  • 5
  • 17