您好,我有以下问题:
我需要请求网络服务。该请求使用 REST POST 方法。所以我需要发送一个 JSON 对象来请求我的数据。
以下代码描述了 JSON Body:
{
"StartTime":"\/Date(928142400000+0200)\/",
"EndTime":"\/Date(928142400000+0200)\/",
"RoomTypes":[0]
}
如您所见,我有两个 JSON 日期时间格式的字段。
这是我到目前为止的 Java 代码:
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", ?);
有什么技巧可以实现这一点吗?
谢谢!
最佳答案
看起来 REST 服务正在等待 Unix 纪元以来的时间。使用Date.getTime()如:json.put("StartTime", "\/Date(" + pStart.getTime() + "+0200)\/");
关于java - 将 Java 日期转换为 JSON 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460572/