没有自定义类的改造日期解析

时间:2018-01-16 11:58:23

标签: java android gson retrofit

在应用程序中,我使用Retrofit来使用api数据。作为转换器我正在使用Gson

new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();

我从api获得如下所示的Date类型。但我无法将其转换为java.util.Date对象。

{
   "date":{
      "year":2018,
      "month":2,
      "day":11
   },
   "time":{
      "hour":22,
      "minute":40,
      "second":0,
      "nano":0
   }
}

为了获得这种类型的日期,我创建了名为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;

我的问题是如何在没有上述自定义类的情况下转换消费数据。我真正想要的是dateformatter。我应该如何处理.setDateFormat("")来处理日期转换。

3 个答案:

答案 0 :(得分:1)

如果你想从gson

设置日期和时间,请尝试这样做
    class Equipment():
        id
        name 
        ...

    class Alert():
        id
         ...
        pin
        value
       equipment_id

答案 1 :(得分:1)

经过一些研究后我发现要将日期反序列化,应该有一个deserializer。当我创建gson对象时,我需要将此deserializer设置为GsonBuilder()的适配器。 @SaravInfer回答和@Hemant评论给了我一个从jsonObject创建日期的线索。

解串器

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;
        }
    }
}

当我创建gson;

new GsonBuilder()
    .registerTypeAdapter(Date.class, new DateDeserializer())
    .create();

答案 2 :(得分:0)

试试我在我的应用中使用的代码

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);

将其转换为日历

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);