Android Time API with Kotlin
Android developers often use Retrofit for networking. Here is a simple interface definition to consume the Time.Now API and map it to a data class.
KOTLIN
data class TimeResponse(
val datetime: String,
val timezone: String,
val unixtime: Long
)
interface TimeApiService {
@GET("timezone/{area}/{location}")
suspend fun getTime(
@Path("area") area: String,
@Path("location") location: String
): TimeResponse
}
// Usage inside a Coroutine
val time = api.getTime("America", "New_York")
Log.d("API", "NYC Time: ${time.datetime}")
Why use Time.Now for Mobile?
- Coroutine support
- Clean architecture
- Type-safe response