I have an Android App written in Kotlin, that gets data from JSON. I use Retrofit and Moshi to retrieve the data. I receive errors because Moshi does not know how to convert a date to GregorianCalendar. How can I convert the JSON date format to the GregorianCalendar format?
The JSON date is in the following format: 2000-06-28T00:00:00
The API configuration:
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()
}
}
}
Example class:
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
)
{
"persons": [{
"personId": 1,
"personName": "Bert",
"personAge": 19,
"isFemale": "false",
"birthDate": "2000-06-28T00:00:00"
}
]
}
I want the JSON date 2000-06-28T00:00:00 to be converted to GregorianCalendar or any other Date format that is not depricated but supported by minimum API21