Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson failed to parse date

Tags:

android

gson

In some of the users in my application throws Fatal Exception: com.google.gson.JsonSyntaxException. I use gson to write and read data to disk LRU cache. How I understand this exception happens when I read data from cache.

This is how I created json instance:

Gson gson = new Gson();

This exception happens in Android 5,6,7,8. This is statistics from Crashlytics.

This is exception stack trace from Crashlytics:

Fatal Exception: com.google.gson.JsonSyntaxException: Nov 5, 2019 20:18:24
       at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.Gson.fromJson(Gson.java:927)
       at com.google.gson.Gson.fromJson(Gson.java:892)
       at com.google.gson.Gson.fromJson(Gson.java:841)
       at com.google.gson.Gson.fromJson(Gson.java:813)
       at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
       at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
       at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
       at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
       at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
       at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.os.HandlerThread.run(HandlerThread.java:61)

Caused by java.text.ParseException: Failed to parse date ["Nov 5, 2019
                20:18:24"]: Invalid number: Nov 
                       at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
       at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.Gson.fromJson(Gson.java:927)
       at com.google.gson.Gson.fromJson(Gson.java:892)
       at com.google.gson.Gson.fromJson(Gson.java:841)
       at com.google.gson.Gson.fromJson(Gson.java:813)
       at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
       at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
       at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
       at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
       at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
       at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.os.HandlerThread.run(HandlerThread.java:61)

Caused by java.lang.NumberFormatException: Invalid number: Nov
                       at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:311)
       at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129)
       at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
       at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
       at com.google.gson.Gson.fromJson(Gson.java:927)
       at com.google.gson.Gson.fromJson(Gson.java:892)
       at com.google.gson.Gson.fromJson(Gson.java:841)
       at com.google.gson.Gson.fromJson(Gson.java:813)
       at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
       at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
       at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
       at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
       at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
       at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.os.HandlerThread.run(HandlerThread.java:61)
like image 948
TikTak Avatar asked Feb 06 '20 07:02

TikTak


1 Answers

Caused by java.text.ParseException: Failed to parse date ["Nov 5, 2019 20:18:24"]: Invalid number: Nov

Try to setup date format for parsing purposes.

Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy HH:mm:ss").create();

like image 128
P.Juni Avatar answered Nov 18 '22 11:11

P.Juni