gson java.text.ParseException:无法解析日期[“2021-05-03 18:01:42”]:时区指示符“”无效

tjrkku2a  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(306)

我的Android程序使用改型和Gson,后端发送给我的响应是“createTime”:“2021-05-03 18:01:42”,当我使用日期类型时,有一个错误显示:

com.google.gson.JsonSyntaxException: 2021-05-03 18:01:42
2021-05-04 19:19:37.426 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
2021-05-04 19:19:37.426 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
2021-05-04 19:19:37.426 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
2021-05-04 19:19:37.427 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
2021-05-04 19:19:37.427 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
2021-05-04 19:19:37.427 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
2021-05-04 19:19:37.427 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
2021-05-04 19:19:37.427 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
2021-05-04 19:19:37.428 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
2021-05-04 19:19:37.428 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:45)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at e.a.l.subscribe(Observable.java:12284)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
2021-05-04 19:19:37.429 20992-20992/com.test W/System.err:     at e.a.l.subscribe(Observable.java:12284)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at e.a.a0.e.d.l3$b.run(ObservableSubscribeOn.java:96)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at e.a.t$a.run(Scheduler.java:578)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at e.a.a0.g.k.run(ScheduledRunnable.java:66)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at e.a.a0.g.k.call(ScheduledRunnable.java:57)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at java.lang.Thread.run(Thread.java:764)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err: Caused by: java.text.ParseException: Failed to parse date ["2021-05-03 18:01:42"]: Invalid time zone indicator ' '
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
2021-05-04 19:19:37.430 20992-20992/com.test W/System.err:  ... 28 more
2021-05-04 19:19:37.431 20992-20992/com.test W/System.err: Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ' '
2021-05-04 19:19:37.431 20992-20992/com.test W/System.err:     at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
2021-05-04 19:19:37.431 20992-20992/com.test W/System.err:  ... 29 more

我的kotlin bean代码是:

class Order {
        var createTime: Date? = null
    }

如何解决此问题?

9wbgstp7

9wbgstp71#

你可以用你的日期格式创建一个Gson示例,但这不是默认的,因为它会崩溃。
创建这样的Gson示例来解析createTime

val g =  GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()

相关问题