我正在尝试将对象Datetime
映射到Date
数据类型这里是我的代码: -
HolidayResponse holidayResponse=null;
app=(HrmsApp)context.getApplicationContext();
HttpClient client = new DefaultHttpClient();
String Year=app.getHolidayYear();
SettingsManager settings = new SettingsManager(context);
String AuthenticationToken=app.getAuthenticationToken();
HttpGet request=new HttpGet(settings.GetHolidayUrl(Year));
HttpResponse response;
HttpEntity entity=null;
try{
request.setHeader("ApplicationToken",AuthenticationToken);
response = client.execute(request);
entity = response.getEntity();
if (entity != null) {
InputStream inStream = entity.getContent();
String s = CommonTasks.convertStreamToString(inStream);
GsonBuilder gsonb = new GsonBuilder();
DateDeserializer ds = new DateDeserializer();
gsonb.registerTypeAdapter(Date.class, ds);
Gson gson = gsonb.create();
Type jsonResponseType = new TypeToken<ArrayList<Holiday>>() {
}.getType();
List<Holiday> holidayList= gson.fromJson(s,jsonResponseType);//Here i am mapping the Value getting through json into Holiday
}
}catch (Exception e) {
e.printStackTrace();
}
这是我的假期课程
public class Holiday {
public String Name;
public String Description;
public Date Holidaydate;
public Holiday()
{}
}
我的Json结果样本
[{"Name":"New Year's Day","Description":"New Year's Day","Holidaydate":"2014-01-01T00:00:00"}]
我正在尝试将Holidaydate
从Json结果映射到假日类Holidaydate
属性Date
类型。
我没有得到如何在映射时解析日期时间。
无论如何,请提前告诉我。
答案 0 :(得分:0)
而不是:
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
您可以使用:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();