0

I need to display date from firebase and not timestamp:

enter image description here

fun getTimeDate() {
    prayerreference = FirebaseDatabase.getInstance().getReference("PrayerInfo").child("time")
    prayerreference!!.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (ds in dataSnapshot.children) {
                    val timestamp = ds.child("PrayerInfo").child("time").getValue(Long::class.java)

                    // timepost.text= DateDifference(0)
                    //val time = prayerreference.toString()
                    timepost.text = timestamp.toString()
                                //System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)
                    //            DateUtils.MINUTE_IN_MILLIS, flags).toString(
                    //Log.d(ContentValues.TAG, getTimeDate(date).toString())
                }

        }
        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(ContentValues.TAG, databaseError.message) //Don't ignore errors!
        }
    })
    //prayerreference!!.child("time").addListenerForSingleValueEvent()
}

It only displays timestamp from firebase but need it to be date on every post.

AEM
  • 1,354
  • 8
  • 20
  • 30

2 Answers2

1

To convert a timestamp (in milliseconds since the epoch) to a Date, you can pass its value to the constructor of Date. So:

timepost.text = new Date(timestamp.toString())

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • How do I capture that timestamp from firebase..? It only works when the timestamp is hardcoded in the project. – Evansito Robbie Jun 03 '22 at 12:19
  • The code you shared to do so looks fine at first glance, so more likely the code doesn't match the data. If you also want our help with that, edit your question to show the data that is being read (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 03 '22 at 13:13
0

I found the solution.

 @SuppressLint("SimpleDateFormat")
fun DateDifference(fromDate: Long): String {
    var diff: Long = 0
    val ms2 = System.currentTimeMillis()
    // get difference in milli seconds
    diff = ms2 - fromDate
    val diffInSec = Math.abs((diff / TimeAgo.second).toInt())
    var difference = ""
    difference = if (diffInSec < TimeAgo.minute) {
        if (diffInSec <= 1) {
            TimeAgo.secAgo
        } else {
            diffInSec.toString() + TimeAgo.secsAgo
        }
    } else if (diffInSec / TimeAgo.hour < 1) {
        if (diffInSec / TimeAgo.minute <= 1) {
            (diffInSec / TimeAgo.minute).toString() + TimeAgo.minAgo
        } else {
            (diffInSec / TimeAgo.minute).toString() + TimeAgo.minsAgo
        }
    } else if (diffInSec / TimeAgo.day < 1) {
        if (diffInSec / TimeAgo.hour <= 1) {
            (diffInSec / TimeAgo.hour).toString() + TimeAgo.hourAgo
        } else {
            (diffInSec / TimeAgo.hour).toString() + TimeAgo.hoursAgo
        }
    } else if (diffInSec / TimeAgo.week < 1) {
        if (diffInSec / TimeAgo.day <= 1) {
            (diffInSec / TimeAgo.day).toString() + TimeAgo.dayAgo
        } else {
            (diffInSec / TimeAgo.day).toString() + TimeAgo.daysAgo
        }
    } else if (diffInSec / TimeAgo.month < 1) {
        if (diffInSec / TimeAgo.week <= 1) {
            (diffInSec / TimeAgo.week).toString() + TimeAgo.weekAgo
        } else {
            (diffInSec / TimeAgo.week).toString() + TimeAgo.weeksAgo
        }
    } else if (diffInSec / TimeAgo.year < 1) {
        if (diffInSec / TimeAgo.month <= 1) {
            (diffInSec / TimeAgo.month).toString() + TimeAgo.monthAgo
        } else {
            (diffInSec / TimeAgo.month).toString() + TimeAgo.monthsAgo
        }
    } else {
        // return date
        val c: Calendar = Calendar.getInstance()
        c.setTimeInMillis(fromDate)
        val format_before = SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss")
        format_before.format(c.getTime())
    }
    Log.e("time difference is: ", "" + difference)
    timepost.text = difference
    return difference
}

Now the on onBindViewHolder:

holder.DateDifference(model.getTime())

create class TimeAgo:

object TimeAgo {
const val monthAgo = " Month z"
const val monthsAgo = " Months ago"
const val weekAgo = " Week ago"
const val weeksAgo = " Weeks ago"
const val daysAgo = " Days ago"
const val dayAgo = " Day ago"
const val hourAgo = " Hour ago"
const val hoursAgo = " Hours ago"
const val minAgo = " Min ago"
const val minsAgo = " Mins ago"
const val secsAgo = " Secs ago"
const val secAgo = "Just Now"
var second = 1000 // milliseconds
var minute = 60
var hour = minute * 60
var day = hour * 24
var week = day * 7
var month = day * 30
var year = month * 12

}

The model class:

class PrayerInfo {
// string variable for
private var Time = Date().time
// an empty constructor is
// required when using
// Firebase Realtime Database.
open fun PrayerInfo() {

}


fun getTime():Long {

        //Time = Date().time

    return Time
}
fun setTime(time: Long) {
    
    this.Time = time
}

}