DI MI
DI MI

Reputation: 245

Converting Java Date to JSON DateTime

Hi i have the following problem:

i need to request a webservice. the request is with REST POST Method. So i need to send a JSON object to request my data.

the following code describes the JSON Body:

{
    "StartTime":"\/Date(928142400000+0200)\/",
    "EndTime":"\/Date(928142400000+0200)\/",
    "RoomTypes":[0]
}

as you can see i have two fields of JSON dateTime format.

here is my Java Code sofar:

SimpleDateFormat pSdf = new SimpleDateFormat("yyyy-mm-dd HH:mm");
Date pStart = null;
Date pEnd = null;
try{
  //now i parse my strings into the Java.Date objects
  pStart = pSdf.parse(sDate + " " + sStartTime);
  pEnd = pSdf.parse(sDate + " " + sEndTime);
}catch (ParseException e1){
  e1.PrintStackTrace();
}

//Now Building the JSON Request Object
JSONObject json = new JSONObject();
json.put("StartTime", ?); //Here i need the JSON dateTime format right?
json.put("EndTime", ?);

any tips how to achieve this?

Thank you!

Upvotes: 0

Views: 9875

Answers (1)

Justin Muller
Justin Muller

Reputation: 1303

It looks like the REST service is expecting the time since the Unix epoch. Use Date.getTime() such as: json.put("StartTime", "\/Date(" + pStart.getTime() + "+0200)\/");

Upvotes: 2

Related Questions