First replace "unknown" with ""
The Gson object that is directly new is a date property that cannot be resolved to "" and needs to be created by Gsonbuilder
Gson ignoredategson=New Gsonbuilder (). Registertypeadapterfactory (new datenulladapterfactory< > ()). Create ();
The Registertypeadapterfactory () method is to add your own adapter to handle certain types. The new datenulladapterfactory.class needs to be written by himself.
Importjava.util.Date;ImportCom.google.gson.Gson;ImportCom.google.gson.TypeAdapter;Importcom.google.gson.TypeAdapterFactory;ImportCom.google.gson.reflect.TypeToken; Public classDatenulladapterfactory<t>Implementstypeadapterfactory {@SuppressWarnings ("Unchecked") Public<T> typeadapter<t> Create (Gson Gson, typetoken<t>type) {Class<T> Rawtype = (class<t>) Type.getrawtype (); if(Rawtype! = Date.)class) { return NULL; } return(typeadapter<t>)NewDatenulladapter (); }}
Importjava.io.IOException;ImportJava.text.DateFormat;Importjava.text.ParseException;Importjava.text.ParsePosition;Importjava.util.Date;ImportJava.util.Locale;ImportCom.google.gson.Gson;Importcom.google.gson.JsonSyntaxException;ImportCom.google.gson.TypeAdapter;Importcom.google.gson.TypeAdapterFactory;ImportCom.google.gson.internal.bind.DateTypeAdapter;Importcom.google.gson.internal.bind.util.ISO8601Utils;ImportCom.google.gson.reflect.TypeToken;ImportCom.google.gson.stream.JsonReader;ImportCom.google.gson.stream.JsonToken;ImportCom.google.gson.stream.JsonWriter;Importcom.zxtc.common.utils.StringUtils; Public classDatenulladapterextendsTypeadapter<date>{ Public Static FinalTypeadapterfactory FACTORY =Newtypeadapterfactory () {@SuppressWarnings ("Unchecked")//We use a runtime check for make sure the ' T ' s equal@Override Public<T> typeadapter<t> Create (Gson Gson, typetoken<t>TypeToken) { returnTypetoken.getrawtype () = = Date.class? (typeadapter<t>)NewDatetypeadapter ():NULL; } }; Private FinalDateFormat Enusformat=dateformat.getdatetimeinstance (Dateformat.default, Dateformat.default, locale.us); Private FinalDateFormat LocalFormat=dateformat.getdatetimeinstance (Dateformat.default, Dateformat.default); @Override PublicDate read (Jsonreader in)throwsIOException {if(In.peek () = =jsontoken.null) {in.nextnull (); return NULL; } String jsonstr=in.nextstring (); if(Stringutils.isblank (jsonstr)) {return NULL; }Else { returndeserializetodate (JSONSTR); } } Private synchronizedDate deserializetodate (String json) {Try { returnLocalformat.parse (JSON); } Catch(ParseException ignored) {}Try { returnEnusformat.parse (JSON); } Catch(ParseException ignored) {}Try { returnIso8601utils.parse (JSON,NewParseposition (0)); } Catch(ParseException e) {Throw Newjsonsyntaxexception (JSON, E); }} @Override Public synchronized voidWrite (Jsonwriter out, Date value)throwsIOException {if(Value = =NULL) {out.nullvalue (); return; } String dateformatasstring=Enusformat.format (value); Out.value (dateformatasstring); } }
There may also be an error: Invalid time zone indicator
Two types of bonding:
New Gsonbuilder (). Registertypeadapterfactory (new datenulladapterfactory<> ()). Setdateformat (" Yyyy-mm-dd HH:mm:ss "). Create ();
Java.text.ParseException:Failed to parse date ["Unknown ']