我有一个用Kotlin编写的Android应用,它从JSON获取数据。我使用Retrofit和Moshi来检索数据。我收到错误消息是因为Moshi不知道如何将日期转换为GregorianCalendar。如何将JSON日期格式转换为GregorianCalendar格式?
JSON日期采用以下格式:2000-06-28T00:00:00
API配置:
package main
import (
"fmt"
)
func c(i int) int {
defer func() { i++ }()
return i
}
func c1() (i int) {
defer func() { i++ }()
return i
}
func c2() (i int) {
defer func() { i++ }()
return 2
}
func main() {
fmt.Println(c(0)) // Prints 0
fmt.Println(c1()) // Prints 1
fmt.Println(c2()) // Prints 3 Thank you icza
}
示例类:
class ApiClient {
companion object {
private const val BASE_URL = "http://10.0.2.2:3000"
fun getClient(): Retrofit {
val okHttpClient = OkHttpClient.Builder().build()
val moshi = Moshi.Builder().build()
return Builder().client(okHttpClient).baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
}
}
data class Person (
@Json(name = "personId")
val personId: Int,
@Json(name = "personName")
val name: String,
@Json(name = "personAge")
val age: Int,
@Json(name = "isFemale")
val isFemale: Boolean,
@Json(name = "birthDate")
val birthDate: GregorianCalendar
)
我希望将JSON日期2000-06-28T00:00:00转换为GregorianCalendar或未描述但受最低API21支持的任何其他Date格式