如何在Java中存储DateTime值

时间:2014-08-05 08:40:44

标签: java json datetime

我正在尝试将对象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类型。 我没有得到如何在映射时解析日期时间。 无论如何,请提前告诉我。

1 个答案:

答案 0 :(得分:0)

而不是:

GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();   

您可以使用:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();