Yes, it is possible but you need to make a network call for it
https://worldtimeapi.org/api/timezone/Asia/Kolkata
it will give you JSON like this
{
"abbreviation": "IST",
"client_ip": "122.170.105.143",
"datetime": "2023-02-15T10:23:26.414176+05:30",
"day_of_week": 3,
"day_of_year": 46,
"dst": false,
"dst_from": null,
"dst_offset": 0,
"dst_until": null,
"raw_offset": 19800,
"timezone": "Asia/Kolkata",
"unixtime": 1676436806,
"utc_datetime": "2023-02-15T04:53:26.414176+00:00",
"utc_offset": "+05:30",
"week_number": 7
}
Once you have JSON in the response variable you can parse it like
var response; //it will contains response
var datetime = response.getString("datetime");
//if you want to parse it in readable format do it like this
val informat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
val outFormat = SimpleDateFormat("dd/MM/yyyy hh:mm:s a")
outputString = outFormat.format(informat.parse(datetime))
you may need to execute the network call in IO Thread you can use this
import in your gradle
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
//your code
CoroutineScope(Dispatchers.IO).launch {
var response =
JSONObject(URL("https://worldtimeapi.org/api/timezone/Asia/Kolkata").readText())
val datetime = response.getString("datetime")
val informat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
val outFormat = SimpleDateFormat("dd/MM/yyyy hh:mm:s a")
var outputString = outFormat.format(informat.parse(datetime))
binding.text.text = outputString
}