I have managed to retrieve timestamp from firebase of each post but how to I capture timestamp of each post and convert it to date

Issue

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.

Solution

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
}

}

Answered By – Evansito Robbie

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published