0

i want to get the date and time without using the systems date

i am using android studio emulator changing emulators date.

ex: todays date is 2 Feb 2023 but i change my devices date to 26 Feb 2023. i still want it to display 2 Feb 2023 of todays date without depending on systems/device date

this is my code

var dateTime: String
val timeZone = TimeZone.getTimeZone("GMT+8")
val calendar = Calendar.getInstance(timeZone)
val simpleDateFormat = SimpleDateFormat("d MMM yyyy", Locale.US)
simpleDateFormat.setTimeZone(timeZone)
dateTime = simpleDateFormat.format(calendar.getTime()).toString()

but it still returns depending on the systems date

sean
  • 5
  • 2
  • Consider not using `TimeZone`, `Calendar` and `SimpleDateFormat`. Those classes are troublesome and long outdated. Use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). If for older Android, then through [desugaring](https://developer.android.com/studio/write/java8-support). – Ole V.V. Feb 16 '23 at 20:03

1 Answers1

0

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
        }
Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
  • when i use this code `val apiResponse = URL("http://worldtimeapi.org/api/timezone/Asia/Kolkata").readText()` it gives me an error of `java.io.IOException: Cleartext HTTP traffic to worldtimeapi.org not permitted` . when change `http` to `https` it also gives me error – sean Feb 15 '23 at 12:31
  • use https in url, i have update my answer – Kartik Agarwal Feb 15 '23 at 12:38
  • i did change it to https but still gives me this eror [error code picture](https://imgur.com/a/npiKLyD) – sean Feb 15 '23 at 12:47
  • run your URL() in the IO Thread instead on main thread using coroutines, or use Retrofit/Volley to get JSON data – Kartik Agarwal Feb 15 '23 at 13:12