1

I have a working recyclerView that shows timestamp values I need to see the date and time if possible. I am fetching data from Firebase's real-time database

this is my database database

this is my fragment

private fun getIncomeData() {
    val id = auth.currentUser?.uid.toString()
    dref = FirebaseDatabase.getInstance().getReference("Incomes").child(id)

    dref.addValueEventListener(object : ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()){
                for (incomeSnapshot in snapshot.children){
                    val income = incomeSnapshot.getValue(IncomeItemModel::class.java)
                    incomeList.add(income!!)

                    val str = incomeSnapshot.child("incomeAmount").getValue(String::class.java)!!

                    val value = str.toInt()

                    totalIncome += value
                }
                income.text = totalIncome.toString()

                incomeRecycler.adapter = incomeAdapter
            }
        }

        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }

    })

}

I can even accept the best way to insert the current time and date in the firebase database just to complete my project

this is my Adapter

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IncomeViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val v = DataBindingUtil.inflate<IncomeItemBinding>(
        inflater, R.layout.income_item, parent, false
    )

    return IncomeViewHolder(v)
}

override fun onBindViewHolder(holder: IncomeViewHolder, position: Int) {
    val newList = incomeList[position]

    holder.v.isIncome = incomeList[position]

    holder.v.root.setOnClickListener{
        val incomeAmount = newList.incomeAmount
        val incomeCategory = newList.incomeCategory
        val incomeNote = newList.incomeNote
        val incomeDate = newList.incomeDate


    }
}

override fun getItemCount(): Int {
    return incomeList.size
}

}

AfroDemoz
  • 39
  • 1
  • 7

2 Answers2

0

just read stored timestamp and convert it to long

val dateAsString = incomeSnapshot.child("incomeDate").getValue(String::class.java)!!
val dateAsLong = dateAsString.toLong()

looks like your value is in milliseconds already (as Android operates with such precision) and convert to Date, then format it

private fun getDateTimeAsFormattedString(dateAsLongInMs: Long): String? {
    try {
        return SimpleDateFormat("MM/dd/yyyy HH:mm").format(Date(dateAsLongInMs))
    } catch (e: Exception) {
        return null // parsing exception
    }
}

more samples how to convert in HERE

use getDateTimeAsFormattedString in e.g. adapter for printing date on every list item. timestamps are probably already parsed in incomeList

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • thank you very much for your response but still, I failed to implement it in my adapter. I have edited my question and added my adapter codes can you start from there – AfroDemoz May 25 '22 at 18:35
  • sadly no, your adapter is so much empty, there is no `View`s for set text, no source of `R.layout.income_item`, no `IncomeViewHolder` and `IconItemModel` models, but there is `OnClickListener` set, which does nothing... (gets some values from model only, doing nothing with them) Looks like your code is far from working/presenting, very UNfinished... I've already answered to posted question, my job is done, now yours is to finish rest of your project – snachmsm May 25 '22 at 19:19
0

Here is a similar question with an answer FirebaseTimestampToTime

Hope it works for you.