Java.text.ParseException:Failed to parse date ["Unknown ']

Source: Internet
Author: User
Tags dateformat locale

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 ']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.