Reputation: 889
In Application, i am using Retrofit
to consume api data. As converter i am using Gson
new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();
I am getting Date
types like below from api. But i cannot convert it to java.util.Date
object.
{
"date":{
"year":2018,
"month":2,
"day":11
},
"time":{
"hour":22,
"minute":40,
"second":0,
"nano":0
}
}
To get this type of date i create new classes named CustomDate, CustomTime, CustomDateTime.
CustomDate
public class CustomDate implements Serializable{
private int year;
private int month;
private int day;
CustomTime
public class CustomTime implements Serializable {
private int hour;
private int minute;
private int second;
private int nano;
CustomDateTime
public class CustomDateTime implements Serializable {
private CustomDate date;
private CustomTime time;
My question is how can i convert consumed data without above custom classes.What i actually want is dateformatter. What should i put .setDateFormat("")
to handle date conversion.
Upvotes: 0
Views: 1133
Reputation: 9
Try this code that i am used in my app
String string = "2018-03-09T03:02:10.823Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = new SimpleDateFormat(pattern).parse(string);
System.out.println(date);
To convert it to Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Upvotes: 0
Reputation: 889
After some research i found that to make date to be deserialized, there should be a deserializer
. When i create gson
object, i needed to set this deserializer
as adapter for GsonBuilder()
. @SaravInfer answer and @Hemant comment gave me a clue to create date from jsonObject.
Deserializer
public class DateDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
Calendar calendar = null;
if (element.isJsonObject()) {
calendar = Calendar.getInstance();
JsonObject jsonObject = element.getAsJsonObject();
if (jsonObject.has("date")) {
JsonElement dateElement = jsonObject.get("date");
if (dateElement.isJsonObject()) {
JsonObject dateObject = dateElement.getAsJsonObject();
JsonElement year = dateObject.get("year");
JsonElement month = dateObject.get("month");
JsonElement day = dateObject.get("day");
calendar.set(Calendar.YEAR, year.getAsInt());
calendar.set(Calendar.MONTH, month.getAsInt() - 1);
calendar.set(Calendar.DAY_OF_MONTH, day.getAsInt());
}
}
if (jsonObject.has("time")) {
JsonElement timeElement = jsonObject.get("time");
JsonObject timeObject = timeElement.getAsJsonObject();
JsonElement hour = timeObject.get("hour");
JsonElement minute = timeObject.get("minute");
JsonElement second = timeObject.get("second");
JsonElement nano = timeObject.get("nano");
calendar.set(Calendar.HOUR_OF_DAY, hour.getAsInt());
calendar.set(Calendar.MINUTE, minute.getAsInt());
calendar.set(Calendar.SECOND, second.getAsInt());
calendar.set(Calendar.MILLISECOND, nano.getAsInt());
} else {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
}
if (calendar != null) {
return calendar.getTime();
} else {
return null;
}
}
}
When i create gson
;
new GsonBuilder()
.registerTypeAdapter(Date.class, new DateDeserializer())
.create();
Upvotes: 1
Reputation: 3388
Try like this if you want to set date and time from the gson
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, CustomDate.date);// your date object.value
calendar.set(Calendar.YEAR, CustomDate.year); //your date object.value
calendar.set(Calendar.MONTH, CustomDate.month); //your date object.value
calendar.set(Calendar.HOUR_OF_DAY, CustomTime.hour); //your time object.value
calendar.set(Calendar.MINUTE, CustomTime.minute);//your time object.value
calendar.set(Calendar.SECOND, CustomTime.second);//your time object.value
System.out.println(calendar.getTime());
Upvotes: 1