如何在Java中存储DateTime值

时间:2022-03-24 04:14:06

I am trying to Map object Datetime to Date Datatype here is my code:-

我试图将对象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();

}

Here is My Holiday Class

这是我的假期课

public class Holiday {
public String Name;
public String Description;

public Date Holidaydate;
public Holiday()
{}
}

My Json Result Sample

我的Json结果样本

[{"Name":"New Year's Day","Description":"New Year's Day","Holidaydate":"2014-01-01T00:00:00"}]

i am trying to map Holidaydate from Json result to Holiday Class Holidaydate Property which is Date type. I am not getting how to parse datetime to date at the time of mapping. is there anyway then tell me thanks in advance.

我正在尝试将Holidaydate从Json结果映射到Holiday Class Holidaydate属性,这是Date类型。我没有得到如何在映射时解析日期时间。无论如何,请提前告诉我。

1 个解决方案

#1


0  

Instead of :

代替 :

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

You could use :

你可以使用:

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

#1


0  

Instead of :

代替 :

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

You could use :

你可以使用:

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