0

In android studio kotlin or firebase how do I get the utc time and date that will be same on everyone's phone.

This is the code for getting utc time:

fun getUtcTimeAndDate(): String {
    val df = SimpleDateFormat("MMM dd, h:mm a")

    df.timeZone = TimeZone.getTimeZone("UTC")
    return df.format(Date())
}

It gets the utc time from your phone but some phones have slightly different utc times.

I want to get the utc time from somewhere that on everyone's phone will be the same.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    you need to get it from backend server. – CTD Apr 27 '22 at 03:01
  • Can I get it from firebase? –  Apr 27 '22 at 03:28
  • @Leo yes you can by using Cloud Functions – Ticherhaz FreePalestine Apr 27 '22 at 03:29
  • https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/ServerTimestamp – CTD Apr 27 '22 at 03:35
  • Did you search? I readily found this one: [How to get current time from internet in android](https://stackoverflow.com/questions/13064750/how-to-get-current-time-from-internet-in-android). There are a number of similar questions with answers. – Ole V.V. Apr 27 '22 at 17:26
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Apr 27 '22 at 17:27

0 Answers0