2011-12-10 52 views
0

嗨,我有以下问题:将Java日期转换为JSON日期时间

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

下面的代码描述了JSON身体:

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

,你可以看到我有JSON DateTime格式的两个领域。

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

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

谢谢!

+0

有一个在JSON没有日期/时间数据格式/类型。 –

回答

2

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

相关问题