Java 在不使用自定义类的情况下改进日期解析

Java 在不使用自定义类的情况下改进日期解析,java,android,gson,retrofit,Java,Android,Gson,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, "min

在应用程序中,我使用
改造
来使用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的新类

海关日期

public class CustomDate implements Serializable{
    private int year;
    private int month;
    private int day;
海关时间

public class CustomTime implements Serializable {
    private int hour;
    private int minute;
    private int second;
    private int nano;
自定义日期时间

public class CustomDateTime implements Serializable {

    private CustomDate date;
    private CustomTime time;

我的问题是如何在没有上述自定义类的情况下转换消耗的数据。我真正想要的是dateformatter。我应该放置什么来处理日期转换。

如果您想从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());

如果您想从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());

经过一些研究,我发现要使日期反序列化,应该有一个
反序列化器。当我创建
gson
对象时,我需要将此
反序列化器设置为
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
对象时,我需要将此
反序列化器设置为
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;
        }
    }
}

请尝试我在应用程序中使用的此代码

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

请尝试我在应用程序中使用的此代码

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

如果不想使用自定义模型类,可以通过JsonArry和JsonObject解析JSON响应。我不是手动解析。我正在使用改装和Gson。正如我在问题中提到的。Gson处理解析。您需要一个映射器将从映射器获取的字符串转换为日期。格森帮不了你是的。我想gson以后不能自己处理。因此,我创建了反序列化程序。贴在下面作为答案。谢谢。如果不想使用自定义模型类,可以通过JsonArry和JsonObject解析JSON响应。我不是手动解析。我正在使用改装和Gson。正如我在问题中提到的。Gson处理解析。您需要一个映射器将从映射器获取的字符串转换为日期。格森帮不了你是的。我想gson以后不能自己处理。因此,我创建了反序列化程序。贴在下面作为答案。谢谢。这不是我问题的答案。我不想使用自定义类。我不会手动转换任何内容。改型将api结果转换为对象。我实际上想要的是DateFormatter,所以您希望正确地接收json作为字符串,并根据json字符串设置日期??您以前使用过改型吗?引用此项以从中获取字符串形式的响应。这不是我问题的答案。我不想使用自定义类。我不会手动转换任何内容。改型将api结果转换为对象。我实际上想要的是DateFormatter,所以您希望正确地接收json作为字符串,并根据json字符串设置日期??您以前使用过改型吗?如果你看json,这不是我的情况下日期的模式谢谢如果你看json,这不是我的情况下日期的模式谢谢