将Java日期转换为JSON DateTime

时间:2011-12-10 22:53:49

标签: java json

您好我有以下问题:

我需要申请网络服务。请求是使用REST POST方法。所以我需要发送一个JSON对象来请求我的数据。

以下代码描述了JSON Body:

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

你可以看到我有两个JSON dateTime格式的字段。

这是我的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", ?);

任何提示如何实现这一目标?

谢谢!

1 个答案:

答案 0 :(得分:2)

看起来REST服务期待自Unix时代以来的时间。使用Date.getTime(),例如:json.put("StartTime", "\/Date(" + pStart.getTime() + "+0200)\/");