I'm trying to call a Webservice with a DateTime format like this : "\/Date(x)\/" where x is the millisecond value since 1970.

How can I generate a DateTime in this format?

Here is what i've done :

dateValidation_long = dateValidation_string.getTime();
String a = '\\/Date(';
String b = ')\\/';
modifierEntiteDeFacturation3C_request.dateValidation = a + dateValidation_long + b;

Here is the result in my console :


Then, i'm trying to JSON.serialize this date, to send it via a Webservice, and this is the result :


The webservice response is not OK because the format is different as expected :

DateTime content '\/Date(1505904139000)\/' does not start with '\/Date(' and end with ')\/' as required for JSON.
  • We need more information : what error do you get ? which pattern is the right one : "/Date(x)/" OR "\/Date(x)\/" ? – Answers_Seeker Sep 20 '17 at 9:03
  • if i'm not escaping this is my error : Illegal string literal: Invalid string literal '\/Date('. Illegal character sequence \/' in string literal. – FabienHuot Sep 20 '17 at 9:06
  • Putting a backslash as an expected pattern whereas it has an escaping behavior in several technologies/languages/whatever........... Backslash has 92 as ASCII code character, try concatenating it without escaping the backslash when you declare String a and b – Answers_Seeker Sep 20 '17 at 9:59
  • @Answers_Seeker I agree with you with this horrible pattern... Even the DateTime format in millisecond for a date... #nocomment But, finally, I found a "crappy" workaround : envelop = envelop.replace('\\\\', '\\'); (After the JSON.Serialize) – FabienHuot Sep 20 '17 at 12:29
  • could you post an answer with your workaround, this could help other users facing similar issues in the future. thanks! – glls Oct 23 '17 at 17:30

To make my code working, here is what i've done :

String envelop     = JSON.serialize(modifierEntiteDeFacturation3C_request);
envelop = envelop.replace('\\\\', '\\');

Before : dateValidation":"\\/Date(1505904534000)\\/"

After dateValidation":"\/Date(1505904534000)\/"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.