title: How to get Firebase Realtime Database server time with Kotlin in Android
body: <p>I need to get Firebase Database current milliseconds epoch time in Kotlin Android app, i tried to push the value to firebase and then read it, i have a button calls a function named pushEndTime() then calls read() but it always get's null value on the first click then on the second click it get's the past value of endTime</p>
  
  <p>this is the code:</p>
  
  <pre><code>private fun pushEndTime(){
      rootRef.child("user/endTime").setValue(ServerValue.TIMESTAMP)
  }  
  </code></pre>
  
  <p>then i read it using</p>
  
  <pre><code>private fun read(){
      rootRef.child("user/endTime").addValueEventListener(
          object : ValueEventListener {
              override fun onDataChange(dataSnapshot: DataSnapshot) {
  
                  endTime = dataSnapshot.value.toString()
  
              }
  
              override fun onCancelled(databaseError: DatabaseError) {
                  Log.w(TAG, "LoginData:onCancelled", databaseError.toException())
                  Toast.makeText(this@HomeFragment.context, "Error! check your internet", Toast.LENGTH_LONG)
                  .show()
              }
          })
  }
  </code></pre>
  
  <p>i use these methods in <code>onActivityCreated</code> like this :</p>
  
  <pre><code>button.setOnClickListener {
      pushEndTime()
      read()
  }
  </code></pre>
  
  <p>i can't put <code>pushEndTime()</code> in <code>onActivityCreated</code>, it has to be inside a function creates endTime once the button is clicked and some other values checked, but i also tried to put it inside on create and still returns null result on the first button click</p>
  
  <p>I found some answers here suggesting to use <code>Firebase.database.ServerValue.TIMESTAMP</code> but it dosen't work for me</p>
  
  <p>this is how i used it: <code>endTime = ServerValue.TIMESTAMP.getValue(".sv")</code>
  but it returns this value and not the epoch time: <code>Timestamp</code></p>
  
  <p>i tried this: <code>Firebase.database.ServerValue.TIMESTAMP.getValue(".sv")</code> but it shows <code>unresolved reference: ServerValue</code> error</p>
  
  <p>also i tried it like this: <code>Firebase.database.reference.ServerValue.TIMESTAMP.getValue(".sv")</code> and the same unresolved reference error</p>
  
  <p><em>com.google.firebase.database.ServerValue</em> is imported</p>
  
  <p>I need to get firebase server time, if there is defiantly no way, how to get the internet time in kotlin?</p>