java - 当包含区域信息时反序列化 LocalDateTime
问题描述
我从服务器收到了一个时间戳,我想将它反序列化为一个LocalDateTime
实例。
我收到的时间戳有时区信息,它类似于这个字符串
2022-03-24T22:44:03.241082Z[Etc/UTC]
当我尝试使用反序列Gson
化它时,出现以下异常
java.time.format.DateTimeParseException: Text '2022-03-24T22' could not be parsed at index 13
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
at myapp.libraries.serialization.decoders.LocalDateTimeDecoder.deserialize(LocalDateTimeDecoder.kt:15)
at myapp.libraries.serialization.decoders.LocalDateTimeDecoder.deserialize(LocalDateTimeDecoder.kt:13)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.google.gson.Gson.fromJson(Gson.java:817)
at myapp.libraries.serialization.SerializationSpec.It must deserialize LocalDateTime(SerializationSpec.kt:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
我的反序列化代码是
class LocalDateTimeDecoder : JsonDeserializer<LocalDateTime> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): LocalDateTime {
return LocalDateTime.parse(json?.asJsonPrimitive?.asString)
}
}
我怎样才能反序列化这个?
解决方案
推荐阅读
- php - 尝试在 Laravel 5.8 中上传图像数组时仅上传最后一张图像
- python - python中的时间模块不测量两行之间的时间差,总是显示0
- javascript - 如何在 Discord.RichEmbed() 中显示或使用变量
- android - 通知未按预期触发
- android - 如何在辅助功能服务中捕捉长按返回按钮?
- java-8 - 通过 JCodec 或类似方法将 Java 8 BufferedImages 转换为 MP4
- prisma - Botium Box CE 游乐场访问权限?
- javascript - laravel(vuejs 和 vuex)在 callResetPassword 中函数的参数太多
- html - 中心内容 - 然后与 Bootstrap 4 左对齐
- python - 不知道为什么我不能用 2D 成功绘制数据的 3D 绘图